May 2018
Intermediate to advanced
412 pages
9h 3m
English
A map is a collection of key/value pairs. A map literal looks like this:
| | %{ key => value, key => value } |
Here are some maps:
| | iex> states = %{ "AL" => "Alabama", "WI" => "Wisconsin" } |
| | %{"AL" => "Alabama", "WI" => "Wisconsin"} |
| | |
| | iex> responses = %{ { :error, :enoent } => :fatal, { :error, :busy } => :retry } |
| | %{{:error, :busy} => :retry, {:error, :enoent} => :fatal} |
| | |
| | iex> colors = %{ :red => 0xff0000, :green => 0x00ff00, :blue => 0x0000ff } |
| | %{blue: 255, green: 65280, red: 16711680} |
In the first case the keys are strings, in the second they’re tuples, and in the third they’re atoms. Although typically all the keys in a map are the same type, that isn’t required.
| | iex> %{ "one" => 1, :two ... |
Read now
Unlock full access