Essays/Short Circuit Boolean
Short circuit boolean operator is a conjunction, which takes two boolean verbs and evaluates u only if necessary.
Short circuit operation is not possible for all boolean operators. For example, not xor requires both arguments, and x-operator will always have to evaluate u. (However, note that x-operator, and its negation, do not have to evaluate v. Likewise, false and true do not have to evaluate either u or v.)
Summary
m b. 2 2$ Verb Name Short Circuit 16 0 0 0 0 0 false 2 : '0:' 17 0 0 0 1 x *. y and / if ^: 18 0 0 1 0 x > y x and not y 2 : 'u`0:@.v' 19 0 0 1 1 x x 2 : 'u' 20 0 1 0 0 x < y y and not x 2 : '-.@u^:v' 21 0 1 0 1 y y 2 : 'v' 22 0 1 1 0 x ~: y xor 2 : 'u ~: v' 23 0 1 1 1 x +. y or / unless 2 : 'u`1:@.v'
2 : 'u^:(-.@v)'
24 1 0 0 0 x +: y not or 2 : '-.@u`0:@.v' 25 1 0 0 1 x = y not xor 2 : 'u = v' 26 1 0 1 0 -. y not y 2 : '-.@v' 27 1 0 1 1 x >: y x or not y 2 : '1:`u@.v' 28 1 1 0 0 -. x not x 2 : '-.@u' 29 1 1 0 1 x <: y y or not x 2 : '-.@u`1:@.v'
2 : '-.@u^:(-.@v)
30 1 1 1 0 x *: y not and 2 : '1:`(-.@u)@.v' 31 1 1 1 1 1 true 2 : '1:'
More variations similar to shown are possible.
The left verb, in general, need not be boolean valued for the short circuit logic to work. However, the right argument may need to be adjusted. For example, box if non-empty: <^:(*@#). Here length is made into boolean.
Test
Second half of boolean operators is negation of rotated first half.
load'strings' sdeb=: [: <;._2 [: ,&' ' deb t=. sdeb '0: u^:v u`0:@.v u -.@u^:v v u~:v u`1:@.v' t=. t,|.sdeb '1: 1:`(-.@u)@.v -.@u`1:@.v -.@u 1:`u@.v -.@v u=v -.@u`0:@.v' test=: 3 : '([ 2 : y ])"0/~ 0 1' <@:>"1 |: (; ,@test)&> t +------------+-------+ |0: |0 0 0 0| |u^:v |0 0 0 1| |u`0:@.v |0 0 1 0| |u |0 0 1 1| |-.@u^:v |0 1 0 0| |v |0 1 0 1| |u~:v |0 1 1 0| |u`1:@.v |0 1 1 1| |-.@u`0:@.v |1 0 0 0| |u=v |1 0 0 1| |-.@v |1 0 1 0| |1:`u@.v |1 0 1 1| |-.@u |1 1 0 0| |-.@u`1:@.v |1 1 0 1| |1:`(-.@u)@.v|1 1 1 0| |1: |1 1 1 1| +------------+-------+ (i.16) -: #.,@test&> t 1
See Also
- Boolean, J Dictionary
- Shortcutted boolean operators, J General Forum
- WikiPedia:Minimal_evaluation, Wikipedia
Discussion
- From former text: For example, behead if non-empty: }.^:(*@#) . Here length is made into boolean. (Note also that behead and behead if non-empty always produce equivalent results for equivalent arguments -- sometimes the best way to implement short circuit logic is to do nothing at all.) -- Raul Miller 2006-05-23 17:09:41
- That was just a bad example -- Oleg Kobchenko <<DateTime(2006-05-24T00:37:49Z)>>