collect-by ( ... seq quot: ( ... obj -- ... key ) -- ... hashtable )
Statistics
Prev:Computing histograms
Next:collect-index-by ( ... seq quot: ( ... obj -- ... key ) -- ... hashtable )


Vocabulary
math.statistics

Inputs and outputs
seqa sequence
quota quotation with stack effect ( ... obj -- ... key )
hashtablea hashtable


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: math math.statistics prettyprint ; { 11 12 13 14 14 13 12 11 } [ odd? ] collect-by .
H{ { f V{ 12 14 14 12 } } { t V{ 11 13 13 11 } } }


Notes
May be named group-by in other languages.

Definition
USING: assocs kernel ;

IN: math.statistics

: collect-by
( ... seq quot: ( ... obj -- ... key ) -- ... hashtable )
[ keep swap ] curry [ push-at ] sequence>hashtable ; inline