cond ( assoc -- )
Factor documentation > Factor handbook > The language > Combinators > Conditional combinators
Prev:? ( ? true false -- true/false )
Next:case ( obj assoc -- )


Vocabulary
combinators

Inputs and outputs
assoca sequence of quotation pairs and an optional quotation


Word description
Calls the second quotation in the first pair whose first quotation yields a true value. A single quotation will always yield a true value.

The following two phrases are equivalent:
{ { [ X ] [ Y ] } { [ Z ] [ T ] } } cond

X [ Y ] [ Z [ T ] [ no-cond ] if ] if


Errors
Throws a no-cond error if none of the test quotations yield a true value.

Examples
USING: combinators io kernel math ; 0 { { [ dup 0 > ] [ drop "positive" ] } { [ dup 0 < ] [ drop "negative" ] } [ drop "zero" ] } cond print
zero


Definition
USING: kernel quotations sequences ;

IN: combinators

: cond ( assoc -- )
[ dup callable? [ drop t ] [ first call ] if ] find nip
[ dup callable? [ call ] [ second call ] if ] [ no-cond ]
if* ;