StrawHat v0.3.0 StrawHat.Utils.Map View Source

Functions for transforming maps, keys and values.

Link to this section Summary

Functions

Transforms a map’s String.t keys to atom() keys

Transforms a map’s String.t values to atom() values

Recursively traverse a map and invoke a function for each key/ value pair that transforms the map

Recursively traverse a map and invoke a function for each key and a function for each value that transform the map

Transforms a map’s atom() keys to String.t keys

Link to this section Functions

Link to this function atomize_keys(map, options \\ [only_existing: true]) View Source

Transforms a map’s String.t keys to atom() keys.

  • map is any Map.t

  • options is a keyword list of options. The available option is:

    • :only_existing which is set to true will only convert the binary key to an atom if the atom already exists. The default is false.
Link to this function atomize_values(map, options \\ [only_existing: false]) View Source

Transforms a map’s String.t values to atom() values.

  • map is any Map.t

  • options is a keyword list of options. The available option is:

    • :only_existing which is set to true will only convert the binary value to an atom if the atom already exists. The default is false.
Link to this function deep_map(map, function) View Source
deep_map(Map.t(), function :: function()) :: Map.t()

Recursively traverse a map and invoke a function for each key/ value pair that transforms the map.

Arguments

  • map is any Map.t

  • function is a function or function reference that is called for each key/value pair of the provided map

Returns

  • The map transformed by the recursive application of function

Example

iex> map = %{a: “a”, b: %{c: “c”}} iex> RobinCare.Web.Map.deep_map map, fn {k, v} -> …> {k, String.upcase(v)} …> end %{a: “A”, b: %{c: “C”}}

Link to this function deep_map(map, key_function, value_function) View Source
deep_map(Map.t(), key_function :: function(), value_function :: function()) ::
  Map.t()

Recursively traverse a map and invoke a function for each key and a function for each value that transform the map.

  • map is any Map.t

  • key_function is a function or function reference that is called for each key of the provided map and any keys of any submaps

  • value_function is a function or function reference that is called for each value of the provided map and any values of any submaps

Returns:

  • The map transformed by the recursive application of key_function and value_function

Examples

Transforms a map’s atom() keys to String.t keys.

  • map is any Map.t

Examples