index deep by
index items into a nested dict
(list xs, function f) -> any
Returns a nested dict
that contains all values from xs
. Each x
in xs
is indexed by a sequence of keys 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 list of strings representing a path of keys where item x
should be found.
In case f(x)
returns the same path of keys for multiple x
, only one of such x
will be indexed in the returned dict.
In such cases it is undefined which x
is preserved in the returned dict.
If f
returns paths p1 and p2, p2 is longer than p1, and p2 starts with p1, the behavior of index_deep_by
is undefined.
In case f(x)
returns nil
or an empty list, 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 index |
f |
function |
A function of the form (any x, any i) -> list that returns the path of keys for source item x at source index i . |