Vocabulary

sequences

Inputs and outputs

Word description

Like reduce, but splits the sequence in half recursively until each sequence is small enough, and calls the quotation on these smaller sequences. If the quotation computes values that depend on the size of their input, such as bignum arithmetic, then this algorithm can be more efficient than using reduce.

Examples

Computing factorial:

Definition

sequences

Inputs and outputs

seq | a sequence |

start | an integer |

quot | a quotation with stack effect ( elt1 elt2 -- newelt ) |

value | an object |

Word description

Like reduce, but splits the sequence in half recursively until each sequence is small enough, and calls the quotation on these smaller sequences. If the quotation computes values that depend on the size of their input, such as bignum arithmetic, then this algorithm can be more efficient than using reduce.

Examples

Computing factorial:

USING: prettyprint sequences math ;
40 iota rest-slice 1 [ * ] binary-reduce .

20397882081197443358640281739902897356800000000

20397882081197443358640281739902897356800000000

Definition

: binary-reduce

( seq start quot: ( elt1 elt2 -- newelt ) -- value )

pick length 0 max 0 swap (binary-reduce) ; inline

( seq start quot: ( elt1 elt2 -- newelt ) -- value )

pick length 0 max 0 swap (binary-reduce) ; inline