Handbook
Glossary
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
seq
a
sequence
map-quot
a
quotation
with stack effect
( ..a elt -- ..a intermediate )
reduce-quot
a
quotation
with stack effect
( ..a prev intermediate -- ..a next )
Outputs
result
an
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
USING:
kernel
;
IN:
sequences
:
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