if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
Factor handbook » The language » Combinators » Conditional combinators

Prev:unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
Next:when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )


Vocabulary
kernel

Inputs
?a generalized boolean
truea quotation with stack effect ( ..a ? -- ..b )
falsea quotation with stack effect ( ..a -- ..b )


Outputs
None

Word description
Alternative conditional form that preserves the cond value if it is true.

If the condition is true, it is retained on the stack before the true quotation is called. Otherwise, the condition is removed from the stack and the false quotation is called.

The following two lines are equivalent:
X [ Y ] [ Z ] if* X dup [ Y ] [ drop Z ] if


Examples
Notice how in this example, the same value is tested by the conditional, and then used in the true branch; the false branch does not need to drop the value because of how if* works:
USING: assocs io kernel math.parser ; IN: scratchpad : curry-price ( meat -- price ) { { "Beef" 10 } { "Chicken" 12 } { "Lamb" 13 } } at ; : order-curry ( meat -- ) curry-price [ "Your order will be " write number>string write " dollars." write ] [ "Invalid order." print ] if* ; "Deer" order-curry
Invalid order.


Definition

: if*
( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
pick [ drop call ] [ 2nip call ] if ; inline