View Source MapX (CommonX v0.5.9)

Some map extensions.

Link to this section Summary

Functions

Transform the keys of a given map to atoms.

Transform the keys of a given map to atoms.

Deletes the entry in map for a specific key.

Fetches the value for a specific key in the given map. If map contains the given key with value value, then {:ok, value} is returned. If map doesn't contain key, :error is returned. Inlined by the compiler.

Gets the value for a specific key in map.

Merges two maps into one, resolving conflicts through the given fun. All keys in map2 will be added to map1. The given function will be invoked when there are duplicate keys; its arguments are key (the duplicate key), value1 (the value of key in map1), and value2 (the value of key in map2). The value returned by fun is used as the value under key in the resulting map.

Creates a map from an enumerable via the given transformation function. Duplicated keys are removed; the latest one prevails.

Transform the keys of a given map to atoms.

Updates the key in map with the given function.

Link to this section Functions

@spec atomize!(%{optional(String.t()) => any()}) :: %{optional(atom()) => any()}

Transform the keys of a given map to atoms.

examples

Examples

iex> MapX.atomize!(%{a: 5})
%{a: 5}
iex> MapX.atomize!(%{"a" => 5})
%{a: 5}
iex> MapX.atomize!(%{"non existing" => 5})
** (ArgumentError) argument error
@spec atomize(%{optional(String.t()) => any()}) :: %{optional(atom()) => any()}

Transform the keys of a given map to atoms.

examples

Examples

iex> MapX.atomize(%{a: 5})
%{a: 5}
iex> MapX.atomize(%{"a" => 5})
%{a: 5}
@spec delete(map(), atom()) :: map()

Deletes the entry in map for a specific key.

If the key does not exist, returns map unchanged.

Inlined by the compiler.

examples

Examples

iex> MapX.delete(%{a: 1, b: 2}, :a)
%{b: 2}

iex> MapX.delete(%{"a" => 1, "b" => 2}, :a)
%{"b" => 2}

iex> MapX.delete(%{b: 2}, :a)
%{b: 2}
@spec fetch(map(), atom()) :: {:ok, Map.value()} | :error

Fetches the value for a specific key in the given map. If map contains the given key with value value, then {:ok, value} is returned. If map doesn't contain key, :error is returned. Inlined by the compiler.

examples

Examples

iex> MapX.fetch(%{a: 1}, :a)
{:ok, 1}
iex> MapX.fetch(%{"a" => 1}, :a)
{:ok, 1}
iex> MapX.fetch(%{a: 1}, :b)
:error
Link to this function

get(map, key, default \\ nil)

View Source
@spec get(map(), atom(), Map.value()) :: Map.value()

Gets the value for a specific key in map.

If key is present in map with value value, then value is returned. Otherwise, default is returned (which is nil unless specified otherwise).

examples

Examples

iex> MapX.get(%{}, :a)
nil

iex> MapX.get(%{a: 1}, :a)
1

iex> MapX.get(%{"a" => 1}, :a)
1

iex> MapX.get(%{a: 1}, :b)
nil

iex> MapX.get(%{a: 1}, :b, 3)
3
@spec merge(
  map(),
  map(),
  (Map.key(), Map.value(), Map.value() -> {:ok, Map.value()} | {:error, any()})
) :: {:ok, map()} | {:error, any()}

Merges two maps into one, resolving conflicts through the given fun. All keys in map2 will be added to map1. The given function will be invoked when there are duplicate keys; its arguments are key (the duplicate key), value1 (the value of key in map1), and value2 (the value of key in map2). The value returned by fun is used as the value under key in the resulting map.

examples

Examples

iex> MapX.merge(%{a: 1, b: 2}, %{a: 3, d: 4}, fn _k, v1, v2 ->
...>   {:ok, v1 + v2}
...> end)
{:ok, %{a: 4, b: 2, d: 4}}
Link to this function

new(enumerable, transform, struct \\ nil)

View Source
@spec new(
  Enumerable.t(),
  (term() -> {:ok, Map.key(), Map.value()} | {:error, term()}),
  module()
) ::
  {:ok, map()} | {:error, term()}

Creates a map from an enumerable via the given transformation function. Duplicated keys are removed; the latest one prevails.

Returning :skip will skip to the next value.

examples

Examples

iex> MapX.new([:a, :b], fn x -> {:ok, x, x} end)
{:ok, %{a: :a, b: :b}}
iex> MapX.new(1..5, &if(rem(&1, 2) == 0, do: :skip, else: {:ok, &1, &1}))
{:ok, %{1 => 1, 3 => 3, 5 => 5}}
@spec stringify(%{optional(atom()) => any()}) :: %{optional(String.t()) => any()}

Transform the keys of a given map to atoms.

examples

Examples

iex> MapX.stringify(%{a: 5})
%{"a" => 5}
iex> MapX.stringify(%{"a" => 5})
%{"a" => 5}
Link to this function

update_if_exists(map, key, fun)

View Source
@spec update_if_exists(map(), Map.key(), (Map.value() -> Map.value())) :: map()

Updates the key in map with the given function.

If key is present in map with value value, fun is invoked with argument value and its result is used as the new value of key. If key is not present in map, then the original map is returned.

examples

Examples

iex> MapX.update_if_exists(%{a: 1}, :a, &(&1 * 2))
%{a: 2}
iex> MapX.update_if_exists(%{a: 1}, :b, &(&1 * 2))
%{a: 1}
iex> MapX.update_if_exists([a: 5], :a, &(&1 * 2))
** (BadMapError) expected a map, got: [a: 5]