Map.get_and_update
You're seeing just the function
get_and_update
, go back to Map module for more information.
Specs
get_and_update( map(), key(), (value() | nil -> {current_value, new_value :: value()} | :pop) ) :: {current_value, new_map :: map()} when current_value: value()
Gets the value from key
and updates it, all in one pass.
fun
is called with the current value under key
in map
(or nil
if key
is not present in map
) and must return a two-element tuple: the current value
(the retrieved value, which can be operated on before being returned) and the
new value to be stored under key
in the resulting new map. fun
may also
return :pop
, which means the current value shall be removed from map
and
returned (making this function behave like Map.pop(map, key)
).
The returned value is a two-element tuple with the current value returned by
fun
and a new map with the updated value under key
.
Examples
iex> Map.get_and_update(%{a: 1}, :a, fn current_value ->
...> {current_value, "new value!"}
...> end)
{1, %{a: "new value!"}}
iex> Map.get_and_update(%{a: 1}, :b, fn current_value ->
...> {current_value, "new value!"}
...> end)
{nil, %{a: 1, b: "new value!"}}
iex> Map.get_and_update(%{a: 1}, :a, fn _ -> :pop end)
{1, %{}}
iex> Map.get_and_update(%{a: 1}, :b, fn _ -> :pop end)
{nil, %{a: 1}}