group by
groups items into a dict of lists
(list xs, function f) -> any
Returns a dict
. Each x
of xs
is collected in a list located at the key given by f(x)
.
If xs
is a list:
If f
accepts one argument, x
is passed.
If f
accepts two arguments, x
and its index are passed.
If xs
is a dict:
If f
accepts one argument, each entry’s value is passed.
If f
accepts two arguments, each entry’s value and key are passed.
The indexing function f
must return a string representing the key to the list where item x
should be placed.
The order of items in collecting lists is undefined.
In case f(x)
returns nil
, the corresponding x
is omitted from the result.
Returns nil
if xs
is nil
.
Throws an error if xs
is neither a list
nor a dict
.
Throws an error if f
is nil
.
Parameters
Name | Type | Description |
---|---|---|
xs |
list or dict | The list or dict of items to group |
f |
function |
A function of the form (any x, any i) -> string that returns the index of the list for source item x at source index i . |