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
atomize(map) View Source
Transform the keys of a given map to atoms.
Examples
iex> MapX.atomize(%{a: 5})
%{a: 5}
iex> MapX.atomize(%{"a" => 5})
%{a: 5}
atomize!(map) View Source
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
delete(map, key) View Source
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}
fetch(map, key) View Source
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
get(map, key, default \\ nil) View Source
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
merge(map1, map2, fun) View Source
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}}
````
new(enumerable, transform, struct \\ nil) View Source
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}}
stringify(map) View Source
Transform the keys of a given map to atoms.
Examples
iex> MapX.stringify(%{a: 5})
%{"a" => 5}
iex> MapX.stringify(%{"a" => 5})
%{"a" => 5}