May 2018
Intermediate to advanced
412 pages
9h 3m
English
The question we most often ask of our maps is, “Do you have the following keys (and maybe values)?” For example, given this map:
| | person = %{ name: "Dave", height: 1.88 } |
Is there an entry with the key :name?
| | iex> %{ name: a_name } = person |
| | %{height: 1.88, name: "Dave"} |
| | iex> a_name |
| | "Dave" |
Are there entries for the keys :name and :height?
| | iex> %{ name: _, height: _ } = person |
| | %{height: 1.88, name: "Dave"} |
Does the entry with key :name have the value "Dave"?
| | iex> %{ name: "Dave" } = person |
| | %{height: 1.88, name: "Dave"} |
Our map does not have the key :weight, so the following pattern match fails:
| | iex> %{ name: _, weight: _ } = person |
| | ** (MatchError) no match of right ... |
Read now
Unlock full access