Handbook
Glossary
1if ( ..a x pred: ( ..a x quot: ( ..a x -- ..b ? ) -- ..b x ? ) true: ( ..b x -- ..c ) false: ( ..b x -- ..c ) -- ..c )
Factor handbook
»
The language
»
Combinators
»
Conditional combinators
Prev:
?unless ( ..a default cond: ( ..a default -- ..a new/f ) false: ( ..a default -- ..a x ) -- ..a default/x )
Next:
2if ( ..a x y pred: ( ..a x y -- ..b x y ? ) true: ( ..b x y -- ..c ) false: ( ..b x y -- ..c ) -- ..c )
Vocabulary
kernel
Inputs
x
an
object
pred
a
quotation
true
a
quotation
false
a
quotation
Outputs
None
Word description
A variant of
if
that takes a
pred
quotation. Calls
1check
on the
pred
quotation to return a boolean and preserves
x
for the
true
or
false
branches, one of which is called.
Examples
Collatz Conjecture calculation:
USING: kernel math prettyprint ; 6 [ even? ] [ 2 / ] [ 3 * 1 + ] 1if .
3
See also
1when
,
1unless
,
2if
,
2when
,
2unless
,
3if
,
3when
,
3unless
Definition
IN:
kernel
:
1if
( ..a x pred: ( ..a x quot: ( ..a x -- ..b ? ) -- ..b x ? ) true: ( ..b x -- ..c ) false: ( ..b x -- ..c ) -- ..c )
[
1check
]
2dip
if
;
inline