I have a function called deep_map that I use in several different projects to simplify certain classes of map operations. On top of that I have other functions like atomize_keys/3 and so on. It might be useful (or it might be not
)
Summary
Recursively traverse a map and invoke a function
that transforms the map for each key/value pair.
Arguments
mapis anyt:map/0functionis a1-arityfunction 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
functionis 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_functionwill be called with the argumentkeyand the value
function will be called with the argumentvalue
- In the case where
optionsis a keyword list of options. The default is[]
Options
:levelindicates the starting (and optionally ending) levels of
the map at which thefunctionis executed. This can
be an integer representing onelevelor a range
indicating a range of levels. The default is1..#{@max_level}:onlyis a term or list of terms or acheck function. If it is a term
or list of terms, thefunctionis only called if thekeyof the
map is equal to the term or in the list of terms. If:onlyis a
check functionthen thecheck functionis passed the{k, v}of
the current branch in themap. It is expected to return atruthy
value that iftruesignals that the argumentfunctionwill be executed.:exceptis a term or list of terms or acheck function. If it is a term
or list of terms, thefunctionis only called if thekeyof the
map is not equal to the term or not in the list of terms. If:exceptis a
check functionthen thecheck functionis passed the{k, v}of
the current branch in themap. It is expected to return atruthy
value that iftruesignals that the argumentfunctionwill not be executed.
Notes
If both the options :only and :except are provided then the function
is called only when a term meets both criteria.
Returns
- The
maptransformed by the recursive application of
function
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}}


















