June 2022
Intermediate to advanced
130 pages
2h 45m
English
Maps are key-value pairs, but they’re built for fast random access. That means you can access any key quickly. In big O notation,[10] the operation is O(Log n) for an n-item map.
Let’s build a map from a list of tuples. In your console, make sure you still have the Keyword called numbers defined. Then, create a new map with Map.new, like this:
| | iex> numbers = [one: 1, two: 2, three: 3] |
| | [one: 1, two: 2, three: 3] |
| | iex> numbers_map = Map.new(numbers) |
| | %{one: 1, three: 3, two: 2} |
That’s straightforward. The syntax of a map is the same as when the keys are atoms. Maps have a leading % and curly braces surrounding the keys and values. If keys are first, you can use the shorthand atom syntax.
If you don’t ...
Read now
Unlock full access