Maps are the go-to key/value data structure in Elixir. They have good performance at all sizes.
Let’s play with the Map API:[16]
| iex> map = %{ name: "Dave", likes: "Programming", where: "Dallas" } |
| %{likes: "Programming", name: "Dave", where: "Dallas"} |
| iex> Map.keys map |
| [:likes, :name, :where] |
| iex> Map.values map |
| ["Programming", "Dave", "Dallas"] |
| iex> map[:name] |
| "Dave" |
| iex> map.name |
| "Dave" |
| iex> map1 = Map.drop map, [:where, :likes] |
| %{name: "Dave"} |
| iex> map2 = Map.put map, :also_likes, "Ruby" |
| %{also_likes: "Ruby", likes: "Programming", name: "Dave", where: "Dallas"} |
| iex> Map.keys map2 |
| [:also_likes, :likes, :name, :where] |
| iex> Map.has_key? map1, :where |
| false |
| iex> { value, updated_map } = Map.pop map2, :also_likes |
| {"Ruby", %{likes: "Programming", name: "Dave", where: "Dallas"}} |
| iex> Map.equal? map, updated_map |
| true |