Map.get_and_update/3
defmodule Map do
def get_and_update(map, key, fun)
end
Gets the value from key
and updates it, all in one pass.
fun
is called with the current value under key
in map
and must return a two-element tuple: the “get” 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.
The returned value is a tuple with the “get” value returned by fun
and a new map with the updated value under key
.
If key
is not present in map
then fun
is called with nil
and its resulting value is used to build the returned value and the new map.
fun
may also return :pop
, which means the current value shall be removed from map
and returned.