Cldr.Map.deep_map
deep_map
, go back to Cldr.Map module for more information.
deep_map(map, function, options \\ [level: %{__struct__: Range, first: 1, last: 1000000, step: 1}, filter: [], reject: [], skip: [], only: [], except: []])
View SourceSpecs
deep_map( map() | list(), function :: function() | {function(), function()}, options :: list() ) :: map() | list()
Recursively traverse a map and invoke a function that transforms the mapfor each key/value pair.
Arguments
map
is anymap/0
function
is a1-arity
function or function reference that is called for each key/value pair of the provided map. It can also be a 2-tuple of the form{key_function, value_function}
- In the case where
function
is a single function it will be called with the 2-tuple argument{key, value}
- In the case where function is of the form
{key_function, value_function}
thekey_function
will be called with the argumentkey
and the value function will be called with the argumentvalue
- In the case where
options
is a keyword list of options. The default is[level: 1..1000000, filter: [], reject: [], skip: [], only: [], except: []]
Options
:level
indicates the starting (and optionally ending) levels of the map at which thefunction
is executed. This can be an integer representing onelevel
or a range indicating a range of levels. The default is1..1000000
:only
is a term or list of terms or acheck function
. If it is a term or list of terms, thefunction
is only called if thekey
of the map is equal to the term or in the list of terms. If:only
is acheck function
then thecheck function
is passed the{k, v}
of the current branch in themap
. It is expected to return atruthy
value that iftrue
signals that the argumentfunction
will be executed.:except
is a term or list of terms or acheck function
. If it is a term or list of terms, thefunction
is only called if thekey
of the map is not equal to the term or not in the list of terms. If:except
is acheck function
then thecheck function
is passed the{k, v}
of the current branch in themap
. It is expected to return atruthy
value that iftrue
signals that the argumentfunction
will not be executed.:filter
is a term or list of terms or acheck function
. If thekey
currently being processed equals the term (or is in the list of terms, or thecheck_function
returns a truthy value) then this branch of the map is processed byfunction
and its output is included in the result.:reject
is a term or list of terms or acheck function
. If thekey
currently being processed equals the term (or is in the list of terms, or thecheck_function
returns a truthy value) then this branch of the map is omitted from the mapped output.:skip
is a term or list of terms or acheck function
. If thekey
currently being processed equals the term (or is in the list of terms, or thecheck_function
returns a truthy value) then this branch of the map is not processed byfunction
but it is included in the mapped result.
Notes
:only
and:except
operate on individual keys whereas:filter
and:filter
and:reject
operator on entire branches of a mapIf both the options
:only
and:except
are provided then thefunction
is called only when aterm
meets both criteria. That means that:except
has priority over:only
.If both the options
:filter
and:reject
are provided then:reject
has priority over:filter
.
Returns
- The
map
transformed by the recursive application offunction
Examples
iex> map = %{a: :a, b: %{c: :c}}
iex> fun = fn
...> {k, v} when is_atom(k) -> {Atom.to_string(k), v}
...> other -> other
...> end
iex> Cldr.Map.deep_map map, fun
%{"a" => :a, "b" => %{"c" => :c}}
iex> map = %{a: :a, b: %{c: :c}}
iex> Cldr.Map.deep_map map, fun, only: :c
%{a: :a, b: %{"c" => :c}}
iex> Cldr.Map.deep_map map, fun, except: [:a, :b]
%{a: :a, b: %{"c" => :c}}
iex> Cldr.Map.deep_map map, fun, level: 2
%{a: :a, b: %{"c" => :c}}