Move pattern matching on structs koan to the later pattern matching module

This commit is contained in:
Felipe Sere
2018-02-02 08:03:12 +00:00
parent 178bb41361
commit 03c6ddc33b
4 changed files with 17 additions and 13 deletions

View File

@@ -28,24 +28,16 @@ defmodule Structs do
assert older.age == ___
end
defmodule Plane do
defstruct passengers: 0, maker: :boeing
end
def plane?(%Plane{}), do: true
def plane?(_), do: false
koan "Or onto the type of the struct itself" do
assert plane?(%Plane{passengers: 417, maker: :boeing}) == ___
assert plane?(%Person{}) == ___
end
koan "Struct can be treated like maps" do
silvia = %Person{age: 22, name: "Silvia"}
assert Map.fetch(silvia, :age) == ___
end
defmodule Plane do
defstruct passengers: 0, maker: :boeing
end
defmodule Airline do
defstruct plane: %Plane{}, name: "Southwest"
end

View File

@@ -101,6 +101,18 @@ defmodule PatternMatching do
assert name == ___
end
defmodule Plane do
defstruct passengers: 0, maker: :boeing
end
def plane?(%Plane{}), do: true
def plane?(_), do: false
koan "...or onto the type of the struct itself" do
assert plane?(%Plane{passengers: 417, maker: :boeing}) == ___
assert plane?(%Animal{}) == ___
end
koan "Structs will even match with a regular map" do
%{name: name} = %Animal{kind: "dog", name: "Max"}
assert name == ___