CommonX v0.5.7 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.
Updates the key
in map
with the given function.
Link to this section Functions
Specs
Transform the keys of a given map to atoms.
Examples
iex> MapX.atomize(%{a: 5})
%{a: 5}
iex> MapX.atomize(%{"a" => 5})
%{a: 5}
Specs
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
Specs
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}
Specs
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
Specs
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
Specs
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}}
````
Specs
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
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}}
Specs
Transform the keys of a given map to atoms.
Examples
iex> MapX.stringify(%{a: 5})
%{"a" => 5}
iex> MapX.stringify(%{"a" => 5})
%{"a" => 5}
Specs
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
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]