map-reduce ( ..a seq map-quot: ( ..a elt -- ..a intermediate ) reduce-quot: ( ..a 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
seqa sequence
map-quota quotation with stack effect ( ..a elt -- ..a intermediate )
reduce-quota quotation with stack effect ( ..a prev intermediate -- ..a next )


Outputs
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 -- ..a intermediate ) reduce-quot: ( ..a prev intermediate -- ..a next ) -- ..a result )
[ [ [ first ] keep ] dip [ dip ] keep ] dip
[ [ [ swap ] ] dip [ dip swap ] curry compose ] dip compose
1 each-from ; inline