Files
elixir-koans/lib/koans/08_maps.ex
Tim Jarratt 00eb17014a Move sigils to later in the lessons
While doing this, I also discovered that
there is also a reference to lists in numbers, but
that might a bit easier for someone to grasp, given
the hint that the koan gives, and the output they
see when they run it.
2017-04-25 22:38:49 +02:00

60 lines
1.6 KiB
Elixir

defmodule Maps do
use Koans
@intro "Maps"
@person %{
first_name: "Jon",
last_name: "Snow",
age: 27,
}
koan "Maps represent structured data, like a person" do
assert @person == %{first_name: ___,
last_name: "Snow",
age: 27 }
end
koan "Fetching a value returns a tuple with ok when it exists" do
assert Map.fetch(@person, :age) == ___
end
koan "Or the atom :error when it doesn't" do
assert Map.fetch(@person, :family) == ___
end
koan "Extending a map is as simple as adding a new pair" do
person_with_hobby = Map.put(@person, :hobby, "Kayaking")
assert Map.fetch(person_with_hobby, :hobby) == ___
end
koan "Put can also overwrite existing values" do
older_person = Map.put(@person, :age, 37)
assert Map.fetch(older_person, :age) == ___
end
koan "Or you can use some syntactic sugar for existing elements" do
younger_person = %{@person | age: 16}
assert Map.fetch(younger_person, :age) == ___
end
koan "Can remove pairs by key" do
without_age = Map.delete(@person, :age)
assert Map.has_key?(without_age, :age) == ___
end
koan "Can merge maps" do
assert Map.merge(%{first_name: "Jon"}, %{last_name: "Snow"}) == ___
end
koan "When merging, the last map wins" do
merged = Map.merge(@person, %{last_name: "Baratheon"})
assert Map.fetch(merged, :last_name) == ___
end
koan "You can also select sub-maps out of a larger map" do
initial = %{first_name: "Jon", last_name: "Snow", age: 15}
assert Map.take(initial, [:first_name, :last_name]) == ___
end
end