Conditional function in APL
apl, conditional-operator, functional-programming
Solution
Yes, there are APL programmers on SO (but not many!).
I think the answer is that there is no standard on this.
For a scalar solution, I use "pick":
x⊃f t
While for a Boolean array I use indexing as you do above:
f t[x]
I always use index origin zero, so there is no need to add 1, and the parens are not needed.
If these are not simple enough, I think you have to cover them with a function named "if". That will also let you put the true and false in the perhaps more natural ordering of t f.
Problem
Is there a symbol or well-known idiom for the conditional function, in any of the APL dialects? I'm sure I'm missing something, because it's such a basic language element. In other languages it's called conditional operator, but I will avoid that term here, because an APL operator is something else entirely. For example C and friends have `x ? T : F` LISPs have `(if x T F)` Python has `T if x else F` and so on. I know modern APLs have `:If` and friends, but they are imperative statements to control program flow: they don't return a value, cannot be used inside an expression and certainly cannot be applied to arrays of booleans. They have a different purpose altogether, which is just fine by me. The only decent expression I could come up with to do a functional selection is `(F T)[⎕IO+x]`, which doesn't look particularly shorthand or readable to me, although it gets the job done, even on arrays: ``` ('no' 'yes')[⎕IO+(⍳5)∘.>(⍳5)] no no no no no yes no no no no yes yes no no no yes yes yes no no yes yes yes yes no ``` I tried to come up with a similar expression using squad `⌷`, but failed miserably on arrays of booleans. Even if I could, it would still have to embed `⎕IO` or an hardcoded 1, which is even worse as far as readability is concerned. Before I go ahead and define my own `if` and use it on every program I will ever write, is there any canon on this? Am I missing an obvious function or operator? (Are there any APL programmers on SO? :-)