loop ( ... pred: ( ... -- ... ? ) -- ... )
Factor handbook » The language » Combinators » Looping combinators

Prev:do ( pred body -- pred body )


Vocabulary
kernel

Inputs
preda quotation


Outputs
None

Word description
Calls the quotation repeatedly until it outputs f.

Notes
A loop inside a fixed-effect callback can preserve values beneath that callback's inputs without including them in its stack effect.

Examples
Loop until we hit a zero:
USING: kernel random math io ; [ "hi" write bl 10 random zero? not ] loop
hi hi hi

A fun loop:
USING: kernel prettyprint math ; 3 [ dup . 7 + 11 mod dup 3 = not ] loop drop
3 10 6 2 9 5 1 8 4 0 7


Definition

: loop ( ... pred: ( ... -- ... ? ) -- ... )
[ call ] keep swap [ loop ] [ drop ] if ; inline recursive