Skip to content

Latest commit

 

History

History
100 lines (70 loc) · 1.63 KB

File metadata and controls

100 lines (70 loc) · 1.63 KB

Elixir Koans - 08 Maps

import ExUnit.Assertions

Intro

person = %{
    first_name: "Jon",
    last_name: "Snow",
    age: 27
}

Maps represent structured data, like a person

assert person == %{first_name: ___, last_name: "Snow", age: 27}

Fetching a value returns a tuple with ok when it exists

assert Map.fetch(person, :age) == ___

Or the atom :error when it doesn't

assert Map.fetch(person, :family) == ___

Extending a map is as simple as adding a new pair

person_with_hobby = Map.put(person, :hobby, "Kayaking")
assert Map.fetch(person_with_hobby, :hobby) == ___

Put can also overwrite existing values

older_person = Map.put(person, :age, 37)
assert Map.fetch(older_person, :age) == ___

Or you can use some syntactic sugar for existing elements

younger_person = %{person | age: 16}
assert Map.fetch(younger_person, :age) == ___

Can remove pairs by key

without_age = Map.delete(person, :age)
assert Map.has_key?(without_age, :age) == ___

Can merge maps

assert Map.merge(%{first_name: "Jon"}, %{last_name: "Snow"}) == ___

When merging, the last map wins

merged = Map.merge(person, %{last_name: "Baratheon"})
assert Map.fetch(merged, :last_name) == ___

You can also select sub-maps out of a larger map

assert Map.take(person, [:first_name, :last_name]) == ___

Next Steps