collect-by ( ... seq quot: ( ... obj -- ... key ) -- ... assoc )


Vocabulary
assocs

Inputs
seqa sequence
quota quotation with stack effect ( ... obj -- ... key )


Outputs
assocan assoc


Word description
Applies a quotation to each element in the input sequence and returns a hashtable of like elements. The keys of this hashtable are the output of quot and the values at each key are the elements that transformed to that key.

Examples
Collect even and odd elements:
USING: assocs math prettyprint ; { 11 12 13 14 14 13 12 11 } [ odd? ] collect-by .
H{ { t V{ 11 13 13 11 } } { f V{ 12 14 14 12 } } }

Collect strings by length:
USING: assocs prettyprint sequences ; { "one" "two" "three" "four" "five" } [ length ] collect-by .
H{ { 3 V{ "one" "two" } } { 4 V{ "four" "five" } } { 5 V{ "three" } } }


See also
group-by

Definition


: collect-by
( ... seq quot: ( ... obj -- ... key ) -- ... assoc )
[ H{ } clone ] 2dip collect-by! ; inline