reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
Factor handbook » The language » Collections » Sequence operations » Sequence combinators

Prev:each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
Next:interleave ( ... seq between quot: ( ... elt -- ... ) -- ... )


Vocabulary
sequences

Inputs
seqa sequence
identityan object
quota quotation with stack effect ( ... prev elt -- ... next )


Outputs
resultthe final result


Word description
Combines successive elements of the sequence using a binary operation, and outputs the final result. On the first iteration, the two inputs to the quotation are identity, and the first element of the sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the sequence.

Examples
USING: math prettyprint sequences ; { 1 5 3 } 0 [ + ] reduce .
9


Definition


: reduce
( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
swapd each ; inline