map-reduce ( ..a seq map-quot: ( ..a elt -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
Factor handbook > The language > Collections > Sequence operations > Sequence combinators

Prev:map-index-as ( ... seq quot: ( ... elt index -- ... newelt ) exemplar -- ... newseq )
Next:accumulate ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final newseq )


Vocabulary
sequences

Inputs and outputs
seqa sequence
map-quota quotation with stack effect ( ..a elt -- ..b intermediate )
reduce-quota quotation with stack effect ( ..b prev intermediate -- ..a next )
resultan object


Word description
Calls map-quot on each element and combines the results using reduce-quot in the same manner as reduce, except that there is no identity element, and the sequence must have a length of at least 1.

Errors
Throws an error if the sequence is empty.

Examples
USING: sequences prettyprint math ; { 1 3 5 } [ sq ] [ + ] map-reduce .
35


Definition


: map-reduce
( ..a seq map-quot: ( ..a elt -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
[ [ dup first ] dip [ call ] keep ] dip compose
swapd 1 each-from ; inline