HELP: binary-reduce
{ $values
- { "seq" sequence } { "start" integer } { "quot" { $quotation "( ... elt1 elt2 -- ... newelt )" } }
+ { "seq" sequence } { "start" integer } { "quot" { $quotation "( elt1 elt2 -- newelt )" } }
{ "value" object } }
{ $description "Like " { $link 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 " { $link reduce } "." }
{ $examples "Computing factorial:"
: nth3-unsafe ( n seq -- a b c )
[ nth2-unsafe ] [ [ 2 + ] dip nth-unsafe ] 2bi ; inline
-: (binary-reduce) ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) from length -- ... value )
+: (binary-reduce) ( seq start quot: ( elt1 elt2 -- newelt ) from length -- value )
#! We can't use case here since combinators depends on
#! sequences
dup 4 < [
PRIVATE>
-: binary-reduce ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) -- ... value )
+: binary-reduce ( seq start quot: ( elt1 elt2 -- newelt ) -- value )
pick length 0 max 0 swap (binary-reduce) ; inline
: cut ( seq n -- before after )