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

Prev:map-reduce ( ..a seq map-quot: ( ..a elt -- ..a intermediate ) reduce-quot: ( ..a prev intermediate -- ..a next ) -- ..a result )
Next:accumulate-as ( ... seq identity quot: ( ... prev elt -- ... next ) exemplar -- ... final newseq )


Vocabulary
sequences

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


Outputs
finalthe final result
newseqa new sequence


Word description
Combines successive elements of the sequence using a binary operation, and outputs a sequence of intermediate results, together with the final result.

The first element of the output sequence is identity. Then, on the first iteration, the two inputs to the quotation are identity and the first element of the input sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the next element of the input sequence.

When given the empty sequence, outputs a new empty sequence together with the identity.

Examples
USING: math prettyprint sequences ; { 2 2 2 2 2 } 0 [ + ] accumulate . .
{ 0 2 4 6 8 } 10


Notes
May be named scan or prefix sum in other languages.

See also
accumulate!, accumulate-as, accumulate*, accumulate*!, accumulate*-as

Definition


: accumulate
( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final newseq )
pick accumulate-as ; inline