CommonX v0.0.5 MapX View Source

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

Link to this section Functions

Link to this function

atomize(map) View Source
atomize(%{optional(String.t()) => any()}) :: %{optional(atom()) => any()}

Transform the keys of a given map to atoms.

Examples

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

atomize!(map) View Source
atomize!(%{optional(String.t()) => any()}) :: %{optional(atom()) => any()}

Transform the keys of a given map to atoms.

Examples

iex> MapX.atomize!(%{a: 5})
%{a: 5}
iex> MapX.atomize!(%{"a" => 5})
%{a: 5}
iex> MapX.atomize!(%{"non existing" => 5})
** (ArgumentError) argument error
Link to this function

delete(map, key) View Source
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

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}
Link to this function

fetch(map, key) View Source
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

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
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

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
Link to this function

merge(map1, map2, fun) View Source
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

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
new(
  Enumerable.t(),
  (term() -> {:ok, Map.key(), Map.value()} | {:error, term()}),
  module()
) :: map()

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

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}}
Link to this function

stringify(map) View Source
stringify(%{optional(atom()) => any()}) :: %{optional(String.t()) => any()}

Transform the keys of a given map to atoms.

Examples

iex> MapX.stringify(%{a: 5})
%{"a" => 5}
iex> MapX.stringify(%{"a" => 5})
%{"a" => 5}