Kernel.get_and_update_in
You're seeing just the macro
get_and_update_in, go back to Kernel module for more information.
Gets a value and updates a nested data structure via the given path.
This is similar to get_and_update_in/3, except the path is extracted
via a macro rather than passing a list. For example:
get_and_update_in(opts[:foo][:bar], &{&1, &1 + 1})Is equivalent to:
get_and_update_in(opts, [:foo, :bar], &{&1, &1 + 1})This also works with nested structs and the struct.path.to.value way to specify
paths:
get_and_update_in(struct.foo.bar, &{&1, &1 + 1})Note that in order for this macro to work, the complete path must always be visible by this macro. See the "Paths" section below.
Examples
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> get_and_update_in(users["john"].age, &{&1, &1 + 1})
{27, %{"john" => %{age: 28}, "meg" => %{age: 23}}}Paths
A path may start with a variable, local or remote call, and must be followed by one or more:
foo[bar]- accesses the keybarinfoo; in casefoois nil,nilis returnedfoo.bar- accesses a map/struct field; in case the field is not present, an error is raised
Here are some valid paths:
users["john"][:age]
users["john"].age
User.all()["john"].age
all_users()["john"].ageHere are some invalid ones:
# Does a remote call after the initial value
users["john"].do_something(arg1, arg2)
# Does not access any key or field
users