The clearest and most versatile idiom for me would be
⍎ ('case1' 'case2' 'case3' ...)[tests]
This acts exactly like a case statement (and by extension like an if/else).
For example:
'/' {⍎ ('+-×÷')['+-*/' ⍳ ⍺] , '/⍵'} 2 .5
yields 4.
If you want to select the corresponding case from a list of tests, you can
either append the boolean results of the tests and search them with iota 1, or
compress nested list of cases.
ack←{⍎('⍵+1' '(''(⍺-1)ack 1'' ''(⍺-1)ack ⍺ ack ⍵-1'')[⍵>0]')[⍺>0]}
That's the Ackermann function in a lambda. The double quotes start to get hard
to read when several switches are nested. Maybe some clever cropping of the
result of quad CR could allow one to write and test multiple nested switches.
Cheers,
Louis
Fun fact about the first example:
If multiple characters are given as left argument, all but the last will be
executed monadically on the result of the last. This behaviour closely
resembles the dyadic one described here:
http://jsoftware.com/help/dictionary/d620.htm
> On 06 Jul 2016, at 03:10, Kacper Gutowski <[email protected]> wrote:
>
> I would argue that using fixed strings and idiomatic construction to
> select one to eval is less complex than conditionally concatenating
> fragments.
>
> Anyway, since other languages conditioned me to avoid eval like the
> plague and power operator can also be used to construct arbitrary
> control flow, I would try doing it this way:
>
> fact←{{⍵ × fact ⍵-1}⍣(⍵>2)⊢1⌈⍵}
>
> If you need separate else branch, though, the idiom Xtian posted is
> probably the clearest way to do it.
>
> -k
>