reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
Factor documentation > 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 and outputs
seqa sequence
identityan object
quota quotation with stack effect ( ... prev elt -- ... next )
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
USING: kernel ;

IN: sequences

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