Wed Oct 2
Structs
%Person{}
right for the first answer? I looked at the code, not the text to answer it.Pattern Matching
assert_raise MatchError, fn ->
[a, b] = [1, 2, 3]
end
koan "The pattern can make assertions about what it expects" do
assert match?([1, _second, _third], [1, 2, 3])
end
Functions
koan "Functions can have default argument values" do
assert repeat_again("Hello ") == "Hello Hello Hello Hello Hello "
assert repeat_again("Hello ", 2) == "Hello Hello "
end
koan "You can build anonymous functions out of any elixir expression by prefixing it with &" do
three_times = &[&1, &1, &1]
assert three_times.("foo") == ["foo", "foo", "foo"]
end
koan "You can pass functions around as arguments. Place an '&' before the name and state the arity" do
assert times_five_and_then(2, &square/1) == 100
end
Enums
koan "Elements can have a lot in common" do
assert Enum.all?([1, 2, 3], &less_than_five?/1) == true
assert Enum.all?([4, 6, 8], &less_than_five?/1) == false
end
koan "You three there, follow me!" do
assert Enum.take([1, 2, 3, 4, 5], 3) == [1, 2, 3]
end
koan "Zip-up in pairs!" do
letters = [:a, :b, :c]
numbers = [1, 2, 3]
assert Enum.zip(letters, numbers) == [a: 1, b: 2, c: 3]
end
koan "When you want to find that one pesky element" do
assert Enum.find([1, 2, 3, 4], &even?/1) == 2
end
Thu Oct 3
Processes
koan "You can ask a process to introduce itself" do
information = Process.info(self())
assert information[:status] == :running
end
Agents
GenServers
koan "Let's use the remaining functions in the external API" do
Laptop.start_link("EL!73")
{_, response} = Laptop.unlock("EL!73")
assert response == "Laptop unlocked!"
Laptop.change_password("EL!73", "Elixir")
{_, response} = Laptop.unlock("EL!73")
assert response == "Incorrect password!"
{_, response} = Laptop.owner_name()
assert response == "Jack Sparrow"
end
Protocols
Comprehensions
koan "A generator specifies how to extract values from a collection" do
collection = [["Hello","World"], ["Apple", "Pie"]]
assert (for [a, b] <- collection, do: "#{a} #{b}") == ["Hello World", "Apple Pie"]
end
koan "You can use multiple generators at once" do
assert (for x <- ["little", "big"], y <- ["dogs", "cats"], do: "#{x} #{y}") == ["little dogs", "little cats", "big dogs", "big cats"]
end