Maps

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, ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.