Move pinning to the back of the kaon and enhance example
This commit is contained in:
@@ -7,24 +7,6 @@ defmodule PatternMatching do
|
|||||||
assert match?(1, ___)
|
assert match?(1, ___)
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "A value can be bound to a variable" do
|
|
||||||
a = 1
|
|
||||||
assert a == ___
|
|
||||||
end
|
|
||||||
|
|
||||||
koan "A variable can be rebound" do
|
|
||||||
a = 1
|
|
||||||
a = 2
|
|
||||||
assert a == ___
|
|
||||||
end
|
|
||||||
|
|
||||||
koan "A variable can be pinned to prevent it from being rebounded" do
|
|
||||||
a = 1
|
|
||||||
assert_raise MatchError, fn() ->
|
|
||||||
^a = ___
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
koan "Patterns can be used to pull things apart" do
|
koan "Patterns can be used to pull things apart" do
|
||||||
[head | tail] = [1, 2, 3, 4]
|
[head | tail] = [1, 2, 3, 4]
|
||||||
|
|
||||||
@@ -117,4 +99,39 @@ defmodule PatternMatching do
|
|||||||
%{name: name} = %Animal{kind: "dog", name: "Max"}
|
%{name: name} = %Animal{kind: "dog", name: "Max"}
|
||||||
assert name == ___
|
assert name == ___
|
||||||
end
|
end
|
||||||
|
|
||||||
|
koan "A value can be bound to a variable" do
|
||||||
|
a = 1
|
||||||
|
assert a == ___
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "A variable can be rebound" do
|
||||||
|
a = 1
|
||||||
|
a = 2
|
||||||
|
assert a == ___
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "A variable can be pinned to prevent it from being rebound" do
|
||||||
|
pinned_variable = 1
|
||||||
|
|
||||||
|
example = fn
|
||||||
|
(^pinned_variable) -> "The number One"
|
||||||
|
(2) -> "The number Two"
|
||||||
|
(number) -> "The number #{number}"
|
||||||
|
end
|
||||||
|
assert example.(1) == ___
|
||||||
|
assert example.(2) == ___
|
||||||
|
assert example.(3) == ___
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "Pinning works on anywhere one would match, including 'case'" do
|
||||||
|
pinned_variable = 1
|
||||||
|
other_variable = 1
|
||||||
|
result = case other_variable do
|
||||||
|
^pinned_variable -> "same"
|
||||||
|
other -> "different #{other}"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert result == ___
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,9 +5,6 @@ defmodule PatternsTests do
|
|||||||
test "Pattern Matching" do
|
test "Pattern Matching" do
|
||||||
answers = [
|
answers = [
|
||||||
1,
|
1,
|
||||||
1,
|
|
||||||
2,
|
|
||||||
2,
|
|
||||||
{:multiple, [1, [2,3,4]]},
|
{:multiple, [1, [2,3,4]]},
|
||||||
[1,2,3,4],
|
[1,2,3,4],
|
||||||
3,
|
3,
|
||||||
@@ -20,6 +17,10 @@ defmodule PatternsTests do
|
|||||||
"dog",
|
"dog",
|
||||||
"Max",
|
"Max",
|
||||||
"Max",
|
"Max",
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
{:multiple, ["The number One", "The number Two", "The number 3"]},
|
||||||
|
"same"
|
||||||
]
|
]
|
||||||
|
|
||||||
test_all(PatternMatching, answers)
|
test_all(PatternMatching, answers)
|
||||||
|
|||||||
Reference in New Issue
Block a user