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
mapis anymap/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[level: 1..1000000, filter: [], reject: [], skip: [], only: [], except: []]
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..1000000: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 acheck functionthen thecheck functionis passed the{k, v}of the current branch in themap. It is expected to return atruthyvalue 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 acheck functionthen thecheck functionis passed the{k, v}of the current branch in themap. It is expected to return atruthyvalue that iftruesignals that the argumentfunctionwill not be executed.:filteris a term or list of terms or acheck function. If thekeycurrently being processed equals the term (or is in the list of terms, or thecheck_functionreturns a truthy value) then this branch of the map is processed byfunctionand its output is included in the result.:rejectis a term or list of terms or acheck function. If thekeycurrently being processed equals the term (or is in the list of terms, or thecheck_functionreturns a truthy value) then this branch of the map is omitted from the mapped output.:skipis a term or list of terms or acheck function. If thekeycurrently being processed equals the term (or is in the list of terms, or thecheck_functionreturns a truthy value) then this branch of the map is not processed byfunctionbut it is included in the mapped result.
Notes
:onlyand:exceptoperate on individual keys whereas:filterand:filterand:rejectoperator on entire branches of a mapIf both the options
:onlyand:exceptare provided then thefunctionis called only when atermmeets both criteria. That means that:excepthas priority over:only.If both the options
:filterand:rejectare provided then:rejecthas priority over:filter.
Returns
- The
maptransformed 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}}