map>assoc ( ... seq quot: ( ... elt -- ... key value ) exemplar -- ... assoc )
Factor documentation > Factor handbook > The language > Collections > Associative mapping operations > Associative mapping combinators
Prev:sift-values ( assoc -- assoc' )
Next:assoc>map ( ... assoc quot: ( ... key value -- ... elt ) exemplar -- ... seq )


Vocabulary
assocs

Inputs and outputs
seqa sequence
quota quotation with stack effect ( ... elt -- ... key value )
exemplaran assoc
assoca new assoc


Word description
Applies the quotation to each element of the sequence, and collects the keys and values into a new assoc having the same type as exemplar.

Definition
USING: arrays kernel sequences ;

IN: assocs

: map>assoc
( ... seq quot: ( ... elt -- ... key value ) exemplar -- ... assoc )
dup sequence?
[ [ [ 2array ] compose ] dip map-as ] [
[ over assoc-size ] dip new-assoc
[ [ swapd set-at ] curry compose each ] keep
] if ; inline