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


Vocabulary
sequences

Inputs and outputs
seqa sequence
startan integer
quota quotation with stack effect ( elt1 elt2 -- newelt )
valuean 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


Definition


: binary-reduce
( seq start quot: ( elt1 elt2 -- newelt ) -- value )
pick length 0 max 0 swap (binary-reduce) ; inline