Key-based access to data structures.
The Access module defines a behaviour for dynamically accessing
keys of any type in a data structure via the data[key] syntax.
Access supports keyword lists (Keyword) and maps (Map) out
of the box. Keywords supports only atoms keys, keys for maps can
be of any type. Both return nil if the key does not exist:
iex> keywords = [a: 1, b: 2]
iex> keywords[:a]
1
iex> keywords[:c]
nil
iex> map = %{a: 1, b: 2}
iex> map[:a]
1
iex> star_ratings = %{1.0 => "★", 1.5 => "★☆", 2.0 => "★★"}
iex> star_ratings[1.5]
"★☆"This syntax is very convenient as it can be nested arbitrarily:
iex> keywords = [a: 1, b: 2]
iex> keywords[:c][:unknown]
nilThis works because accessing anything on a nil value, returns
nil itself:
iex> nil[:a]
nilMaps and structs
While the access syntax is allowed in maps via map[key],
if your map is made of predefined atom keys, you should prefer
to access those atom keys with map.key instead of map[key],
as map.key will raise if the key is missing (which is not
supposed to happen if the keys are predefined) or if map is
nil.
Similarly, since structs are maps and structs have predefined
keys, they only allow the struct.key syntax and they do not
allow the struct[key] access syntax.
In other words, the map[key] syntax is loose, returning nil
for missing keys, while the map.key syntax is strict, raising
for both nil values and missing keys.
To bridge this gap, Elixir provides the get_in/1 and get_in/2
functions, which are capable of traversing nested data structures,
even in the presence of nils:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> get_in(users["john"].age)
27
iex> get_in(users["unknown"].age)
nilNotice how, even if no user was found, get_in/1 returned nil.
Outside of get_in/1, trying to access the field .age on nil
would raise.
The get_in/2 function takes one step further by allowing
different accessors to be mixed in. For example, given a user
map with the :name and :languages keys, here is how to
access the name of all programming languages:
iex> languages = [
...> %{name: "elixir", type: :functional},
...> %{name: "c", type: :procedural}
...> ]
iex> user = %{name: "john", languages: languages}
iex> get_in(user, [:languages, Access.all(), :name])
["elixir", "c"]This module provides convenience functions for traversing other structures, like tuples and lists. As we will see next, they can even be used to update nested data structures.
If you want to learn more about the dual nature of maps in Elixir,
as they can be either for structured data or as a key-value store,
see the Map module.
Updating nested data structures
The access syntax can also be used with the Kernel.put_in/2,
Kernel.update_in/2, Kernel.get_and_update_in/2, and Kernel.pop_in/1
macros to further manipulate values in nested data structures:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> put_in(users["john"].age, 28)
%{"john" => %{age: 28}, "meg" => %{age: 23}}As shown in the previous section, you can also use the
Kernel.put_in/3, Kernel.update_in/3, Kernel.pop_in/2, and
Kernel.get_and_update_in/3 functions to provide nested
custom accessors. For instance, given a user map with the
:name and :languages keys, here is how to deeply traverse
the map and convert all language names to uppercase:
iex> languages = [
...> %{name: "elixir", type: :functional},
...> %{name: "c", type: :procedural}
...> ]
iex> user = %{name: "john", languages: languages}
iex> update_in(user, [:languages, Access.all(), :name], &String.upcase/1)
%{
name: "john",
languages: [
%{name: "ELIXIR", type: :functional},
%{name: "C", type: :procedural}
]
}See the functions key/1, key!/1, elem/1, and all/0 for
some of the available accessors.
Summary
Callbacks
Invoked in order to access the value stored under key in the given term term.
Invoked in order to access the value under key and update it at the same time.
Invoked to "pop" the value under key out of the given data structure.
Functions
Returns a function that accesses all the elements in a list.
Returns a function that accesses the element at index (zero based) of a list.
Same as at/1 except that it raises Enum.OutOfBoundsError
if the given index is out of bounds.
Returns a function that accesses the element at the given index in a tuple.
Fetches the value for the given key in a container (a map, keyword
list, or struct that implements the Access behaviour).
Returns a function that accesses all elements of a list that match the provided predicate.
Returns a function that accesses the first element of a list that matches the provided predicate.
Gets the value for the given key in a container (a map, keyword
list, or struct that implements the Access behaviour).
Gets and updates the given key in a container (a map, a keyword list,
a struct that implements the Access behaviour).
Returns a function that accesses the given key in a map/struct.
Returns a function that accesses the given key in a map/struct.
Removes the entry with a given key from a container (a map, keyword
list, or struct that implements the Access behaviour).
Returns a function that accesses all items of a list that are within the provided range.
Returns a function that accesses all values in a map or a keyword list.
Types
@type access_fun(data, current_value) :: get_fun(data) | get_and_update_fun(data, current_value)
@type key() :: any()
@type nil_container() :: nil
@type t() :: container() | nil_container() | any()