Keyword.get_and_update
You're seeing just the function
get_and_update
, go back to Keyword module for more information.
Specs
get_and_update( t(), key(), (value() | nil -> {current_value, new_value :: value()} | :pop) ) :: {current_value, new_keywords :: t()} when current_value: value()
Gets the value from key
and updates it, all in one pass.
This fun
argument receives the value of key
(or nil
if key
is not present) 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
. The fun
may also
return :pop
, implying the current value shall be removed from the
keyword list and returned.
The returned value is a tuple with the current value returned by
fun
and a new keyword list with the updated value under key
.
Examples
iex> Keyword.get_and_update([a: 1], :a, fn current_value ->
...> {current_value, "new value!"}
...> end)
{1, [a: "new value!"]}
iex> Keyword.get_and_update([a: 1], :b, fn current_value ->
...> {current_value, "new value!"}
...> end)
{nil, [b: "new value!", a: 1]}
iex> Keyword.get_and_update([a: 1], :a, fn _ -> :pop end)
{1, []}
iex> Keyword.get_and_update([a: 1], :b, fn _ -> :pop end)
{nil, [a: 1]}