Move pattern matching on structs koan to the later pattern matching module
This commit is contained in:
@@ -28,24 +28,16 @@ defmodule Structs do
|
|||||||
assert older.age == ___
|
assert older.age == ___
|
||||||
end
|
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
|
koan "Struct can be treated like maps" do
|
||||||
silvia = %Person{age: 22, name: "Silvia"}
|
silvia = %Person{age: 22, name: "Silvia"}
|
||||||
|
|
||||||
assert Map.fetch(silvia, :age) == ___
|
assert Map.fetch(silvia, :age) == ___
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defmodule Plane do
|
||||||
|
defstruct passengers: 0, maker: :boeing
|
||||||
|
end
|
||||||
|
|
||||||
defmodule Airline do
|
defmodule Airline do
|
||||||
defstruct plane: %Plane{}, name: "Southwest"
|
defstruct plane: %Plane{}, name: "Southwest"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -101,6 +101,18 @@ defmodule PatternMatching do
|
|||||||
assert name == ___
|
assert name == ___
|
||||||
end
|
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
|
koan "Structs will even match with a regular map" do
|
||||||
%{name: name} = %Animal{kind: "dog", name: "Max"}
|
%{name: name} = %Animal{kind: "dog", name: "Max"}
|
||||||
assert name == ___
|
assert name == ___
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ defmodule PatternsTests do
|
|||||||
{:multiple, ["Mickey", "Donald", "I need a name!"]},
|
{:multiple, ["Mickey", "Donald", "I need a name!"]},
|
||||||
"dog",
|
"dog",
|
||||||
"Max",
|
"Max",
|
||||||
|
{:multiple, [true, false]},
|
||||||
"Max",
|
"Max",
|
||||||
1,
|
1,
|
||||||
2,
|
2,
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ defmodule StructsTests do
|
|||||||
nil,
|
nil,
|
||||||
"Joe",
|
"Joe",
|
||||||
33,
|
33,
|
||||||
{:multiple, [true, false]},
|
|
||||||
{:ok, 22},
|
{:ok, 22},
|
||||||
%Structs.Airline{plane: %Structs.Plane{maker: :airbus}, name: "Southwest"},
|
%Structs.Airline{plane: %Structs.Plane{maker: :airbus}, name: "Southwest"},
|
||||||
%Structs.Airline{plane: %Structs.Plane{maker: :boeing, passengers: 202}, name: "Southwest"},
|
%Structs.Airline{plane: %Structs.Plane{maker: :boeing, passengers: 202}, name: "Southwest"},
|
||||||
|
|||||||
Reference in New Issue
Block a user