reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )

Factor handbook » The language » Collections » Sequence operations » Sequence combinators

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

Outputs

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

Definition

sequences

Inputs

seq | a sequence |

identity | an object |

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

Outputs

result | the 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

9

Definition