Improve punctuation in PatternMatching koans
This commit is contained in:
@@ -1,84 +1,71 @@
|
|||||||
defmodule PatternMatching do
|
defmodule PatternMatching do
|
||||||
use Koans
|
use Koans
|
||||||
|
|
||||||
koan "one matches one" do
|
koan "One matches one" do
|
||||||
assert match?(1, :__)
|
assert match?(1, :__)
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "a pattern can change" do
|
koan "A pattern can change" do
|
||||||
a = 1
|
a = 1
|
||||||
assert a = :__
|
assert a = :__
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO not sure about this koan?
|
koan "A pattern can also be strict" do
|
||||||
koan "a pattern can also be strict" do
|
|
||||||
a = 1
|
a = 1
|
||||||
assert ^a = :__
|
assert ^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]
|
||||||
|
|
||||||
assert head == :__
|
assert head == :__
|
||||||
end
|
|
||||||
|
|
||||||
koan "...whichever side you actually need" do
|
|
||||||
[_head | tail] = [1,2,3,4]
|
|
||||||
|
|
||||||
assert tail == :__
|
assert tail == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
|
koan "And then put them back together" do
|
||||||
koan "and then put them back together" do
|
|
||||||
head = 1
|
head = 1
|
||||||
tail = [2,3,4]
|
tail = [2,3,4]
|
||||||
|
|
||||||
assert :__ == [head | tail]
|
assert :__ == [head | tail]
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "Some values can be irrelevant" do
|
koan "Some values can be ignored" do
|
||||||
[_first, _second, third, _fourth] = [1,2,3,4]
|
[_first, _second, third, _fourth] = [1,2,3,4]
|
||||||
|
|
||||||
assert third == :__
|
assert third == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "strings come apart just a easily" do
|
koan "Strings come apart just a easily" do
|
||||||
"Shopping list: " <> items = "Shopping list: eggs, milk"
|
"Shopping list: " <> items = "Shopping list: eggs, milk"
|
||||||
|
|
||||||
assert items == :__
|
assert items == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "patterns show what you really care about" do
|
koan "Patterns show what you really care about" do
|
||||||
%{make: make} = %{type: "car", year: 2016, make: "Honda", color: "black"}
|
%{make: make} = %{type: "car", year: 2016, make: "Honda", color: "black"}
|
||||||
|
|
||||||
assert make == :__
|
assert make == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "the pattern can make assertions about what it expects" do
|
koan "The pattern can make assertions about what it expects" do
|
||||||
assert match?([1, _, _], :__)
|
assert match?([1, _second, _third], :__)
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_noise(%{type: "cat"}), do: "Meow"
|
def make_noise(%{type: "cat"}), do: "Meow"
|
||||||
def make_noise(%{type: "dog"}), do: "Woof"
|
def make_noise(%{type: "dog"}), do: "Woof"
|
||||||
def make_noise(_anything), do: "Eh?"
|
def make_noise(_anything), do: "Eh?"
|
||||||
|
|
||||||
koan "functions can declare what kind of arguments they accept" do
|
koan "Functions can declare what kind of arguments they accept" do
|
||||||
dog = %{type: "dog", legs: 4, age: 9, color: "brown"}
|
dog = %{type: "dog", legs: 4, age: 9, color: "brown"}
|
||||||
|
cat = %{type: "cat", legs: 4, age: 3, color: "grey"}
|
||||||
|
snake = %{type: "snake", legs: 0, age: 20, color: "black"}
|
||||||
|
|
||||||
assert make_noise(dog) == :__
|
assert make_noise(dog) == :__
|
||||||
end
|
|
||||||
|
|
||||||
koan "...and for cats..." do
|
|
||||||
cat = %{type: "cat", legs: 4, age: 3, color: "grey"}
|
|
||||||
assert make_noise(cat) == :__
|
assert make_noise(cat) == :__
|
||||||
end
|
|
||||||
|
|
||||||
koan "...and for snakes..." do
|
|
||||||
snake = %{type: "snake", legs: 0, age: 20, color: "black"}
|
|
||||||
assert make_noise(snake) == :__
|
assert make_noise(snake) == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "errors are shaped differently than sucessful results" do
|
koan "Errors are shaped differently than sucessful results" do
|
||||||
result = case Map.fetch(%{}, :obviously_not_a_key) do
|
result = case Map.fetch(%{}, :obviously_not_a_key) do
|
||||||
:error -> "not present"
|
:error -> "not present"
|
||||||
_ -> flunk("I should not happen")
|
_ -> flunk("I should not happen")
|
||||||
|
|||||||
@@ -124,16 +124,13 @@ defmodule KoansHarnessTest do
|
|||||||
1,
|
1,
|
||||||
2,
|
2,
|
||||||
1,
|
1,
|
||||||
1,
|
{:multiple, [1, [2,3,4]]},
|
||||||
[2,3,4],
|
|
||||||
[1,2,3,4],
|
[1,2,3,4],
|
||||||
3,
|
3,
|
||||||
"eggs, milk",
|
"eggs, milk",
|
||||||
"Honda",
|
"Honda",
|
||||||
[1,2,3],
|
[1,2,3],
|
||||||
"Woof",
|
{:multiple, ["Woof", "Meow", "Eh?",]},
|
||||||
"Meow",
|
|
||||||
"Eh?",
|
|
||||||
"not present"
|
"not present"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user