Conditional combinators
Factor handbook » The language » Combinators

Prev:Dataflow combinators
Next:Looping combinators


The basic conditionals:
if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b )

when ( ..a ? true: ( ..a -- ..a ) -- ..a )

unless ( ..a ? false: ( ..a -- ..a ) -- ..a )


Forms abstracting a common stack shuffle pattern:
if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )

when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )

unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )


Another form abstracting a common stack shuffle pattern:
?if ( ..a default cond: ( default -- new/f ) true: ( ..a new -- ..b ) false: ( ..a default -- ..b ) -- ..b )

?when ( ..a default cond: ( ..a default -- ..a new/f ) true: ( ..a new -- ..a x ) -- ..a default/x )

?unless ( ..a default cond: ( ..a default -- ..a new/f ) false: ( ..a default -- ..a x ) -- ..a default/x )


Sometimes instead of branching, you just need to pick one of two values:
? ( ? true false -- true/false )


Two combinators which abstract out nested chains of if:
cond ( assoc -- )

case ( obj assoc -- )


Expressing conditionals with boolean logic

See also
Booleans, Bitwise arithmetic, both?, either?