Map.merge
merge
, go back to Map module for more information.
Specs
Merges two maps into one.
All keys in map2
will be added to map1
, overriding any existing one
(i.e., the keys in map2
"have precedence" over the ones in map1
).
If you have a struct and you would like to merge a set of keys into the
struct, do not use this function, as it would merge all keys on the right
side into the struct, even if the key is not part of the struct. Instead,
use Kernel.struct/2
.
Inlined by the compiler.
Examples
iex> Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4})
%{a: 3, b: 2, d: 4}
Specs
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> Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4}, fn _k, v1, v2 ->
...> v1 + v2
...> end)
%{a: 4, b: 2, d: 4}