accumulate! ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final seq )

Factor handbook » The language » Collections » Sequence operations » Destructive sequence operations

Factor handbook » The language » Collections » Sequence operations » Destructive sequence operations

Prev: | map! ( ... seq quot: ( ... elt -- ... newelt ) -- ... seq ) |

Next: | accumulate*! ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... seq ) |

Vocabulary

sequences

Inputs

Outputs

Word description

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

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

When given the empty sequence, outputs the same empty sequence together with the identity.

Errors

Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by quot.

Side effects

Modifies seq

Examples

Notes

May be named scan or prefix sum in other languages.

See also

accumulate, accumulate-as, accumulate*, accumulate*!, accumulate*-as

Definition

sequences

Inputs

seq | a mutable sequence |

identity | an object |

quot | a quotation with stack effect ( ... prev elt -- ... next ) |

Outputs

seq | a mutable sequence |

final | the final result |

Word description

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

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

When given the empty sequence, outputs the same empty sequence together with the identity.

Errors

Throws an error if the sequence is immutable, or the sequence cannot hold elements of the type output by quot.

Side effects

Modifies seq

Examples

USING: math prettyprint sequences ;
{ 2 2 2 2 2 } 0 [ + ] accumulate! . .

{ 0 2 4 6 8 } 10

{ 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 seq )

(accumulate) map! ; inline

( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final seq )

(accumulate) map! ; inline