produce ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) -- ..b seq )
Factor handbook » The language » Collections » Sequence operations » Sequence combinators

Prev:replicate-as ( ... len quot: ( ... -- ... newelt ) exemplar -- ... newseq )
Next:produce-as ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) exemplar -- ..b seq )


Vocabulary
sequences

Inputs
preda quotation with stack effect ( ..a -- ..b ? )
quota quotation with stack effect ( ..b -- ..a obj )


Outputs
seqa sequence


Word description
Calls pred repeatedly. If the predicate yields f, stops, otherwise, calls quot to yield a value. Values are accumulated and returned in a sequence at the end.

Examples
The following example divides a number by two until we reach zero, and accumulates intermediate results:
USING: kernel math prettyprint sequences ; 1337 [ dup 0 > ] [ 2/ dup ] produce nip .
{ 668 334 167 83 41 20 10 5 2 1 0 }

The following example collects random numbers as long as they are greater than 1:
USING: kernel prettyprint random sequences ; [ 10 random dup 1 > ] [ ] produce nip .
{ 8 2 2 9 }


Definition

: produce
( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) -- ..b seq )
{ } produce-as ; inline