when

check conditions one by one and execute corresponding block accordingly


Parameters

when conditions :block

Attributes

any check all conditions, without breaking, regardless of success
has :any prepend given value to each of the conditions

Returns

  • :logical

Examples

copy
; the main block is always evaluated! when [ prime? 4 -> print "yes, 4 is prime - wait, what?!" prime? 5 -> print "yes, 5 is prime" prime? 7 -> print "yes, 6 is prime" true -> print "none of the above was true" ] ; yes, 5 is prime

copy
when.any [ prime? 4 -> print "yes, 4 is prime - wait, what?!" prime? 5 -> print "yes, 5 is prime" prime? 7 -> print "yes, 7 is prime" ] ; yes, 5 is prime ; yes, 7 is prime

copy
x: 2 when.has: x [ [=0] -> print "x is zero!" [<1] -> print "x is less than 1" [<4] -> print "x is less than 4" true -> print "x is >= 4" ] ; x is less than 4

copy
f: function [x][ print ["called F with:" x] return odd? x ] ; short-circuiting: ; conditions are not evaluated unless needed when [ [f 10]-> print "F 10" [f 11]-> print "F 11" [f 12]-> print "F 12" ] ; called F with: 10 ; called F with: 11 ; F 11

Related