Skip to content

Commit 03c6ddc

Browse files
committed
Move pattern matching on structs koan to the later pattern matching module
1 parent 178bb41 commit 03c6ddc

4 files changed

Lines changed: 17 additions & 13 deletions

File tree

lib/koans/10_structs.ex

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,16 @@ defmodule Structs do
2828
assert older.age == ___
2929
end
3030

31-
defmodule Plane do
32-
defstruct passengers: 0, maker: :boeing
33-
end
34-
35-
def plane?(%Plane{}), do: true
36-
def plane?(_), do: false
37-
38-
koan "Or onto the type of the struct itself" do
39-
assert plane?(%Plane{passengers: 417, maker: :boeing}) == ___
40-
assert plane?(%Person{}) == ___
41-
end
42-
4331
koan "Struct can be treated like maps" do
4432
silvia = %Person{age: 22, name: "Silvia"}
4533

4634
assert Map.fetch(silvia, :age) == ___
4735
end
4836

37+
defmodule Plane do
38+
defstruct passengers: 0, maker: :boeing
39+
end
40+
4941
defmodule Airline do
5042
defstruct plane: %Plane{}, name: "Southwest"
5143
end

lib/koans/12_pattern_matching.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ defmodule PatternMatching do
101101
assert name == ___
102102
end
103103

104+
defmodule Plane do
105+
defstruct passengers: 0, maker: :boeing
106+
end
107+
108+
def plane?(%Plane{}), do: true
109+
def plane?(_), do: false
110+
111+
koan "...or onto the type of the struct itself" do
112+
assert plane?(%Plane{passengers: 417, maker: :boeing}) == ___
113+
assert plane?(%Animal{}) == ___
114+
end
115+
104116
koan "Structs will even match with a regular map" do
105117
%{name: name} = %Animal{kind: "dog", name: "Max"}
106118
assert name == ___

test/koans/patterns_koans_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ defmodule PatternsTests do
1717
{:multiple, ["Mickey", "Donald", "I need a name!"]},
1818
"dog",
1919
"Max",
20+
{:multiple, [true, false]},
2021
"Max",
2122
1,
2223
2,

test/koans/structs_koans_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule StructsTests do
88
nil,
99
"Joe",
1010
33,
11-
{:multiple, [true, false]},
1211
{:ok, 22},
1312
%Structs.Airline{plane: %Structs.Plane{maker: :airbus}, name: "Southwest"},
1413
%Structs.Airline{plane: %Structs.Plane{maker: :boeing, passengers: 202}, name: "Southwest"},

0 commit comments

Comments
 (0)