Extract answers from Functions module

This commit is contained in:
Felipe Sere
2016-03-13 11:45:03 +00:00
parent 5509d8a4eb
commit 353721412f
2 changed files with 62 additions and 24 deletions

View File

@@ -6,12 +6,12 @@ defmodule Functions do
end end
# A function starts with 'def', has a 'do-end' pair # A function starts with 'def', has a 'do-end' pair
koan "it returns the last statement that was called" do koan "it returns the last statement that was called" do
assert inside() == :light assert inside() == :__
end end
def quick_inline_product(a, b), do: a * b def quick_inline_product(a, b), do: a * b
koan "Short functions can be defined in a single line, but mind the comman and colon!" do koan "Short functions can be defined in a single line, but mind the comman and colon!" do
assert quick_inline_product(2,3) == 6 assert quick_inline_product(2,:__) == 6
end end
# A function can have an argument between parentheses, after the name # A function can have an argument between parentheses, after the name
@@ -23,9 +23,12 @@ defmodule Functions do
end end
end end
koan "Can return different things" do koan "Can return atoms..." do
assert will_change(true) == :it_was_truthy assert will_change(true) == :__
assert will_change(false) == "It really wasn't" end
koan "..or strings from the same function!" do
assert will_change(false) == :__
end end
def repeat(times, message) do def repeat(times, message) do
@@ -33,64 +36,73 @@ defmodule Functions do
end end
koan "Functions can have more than one arguement" do koan "Functions can have more than one arguement" do
assert repeat(3, "Hello ") == "Hello Hello Hello " assert repeat(3, "Hello ") == :__
end end
def repeat_again(times \\ 3, message) do def repeat_again(times \\ 5, message) do
String.duplicate(message, times) String.duplicate(message, times)
end end
koan "But sometimes, you may want to default some arguments" do koan "But sometimes, you may want to default some arguments" do
assert repeat_again("Hello ") == "Hello Hello Hello " assert repeat_again("Hello ") == :__
end end
def first(foo, bar), do: "#{foo} and #{bar}" def first(foo, bar), do: "#{foo} and #{bar}"
def first(foo), do: "only #{foo}" def first(foo), do: "only #{foo}"
koan "Functions are distinguished by name and arity - the number of arguments" do koan "functions with the same name are distinguished by arity" do
assert first("One", "Two") == "One and Two" assert first("One", "Two") == :__
assert first("One") == "only One"
end end
koan "the name stays - the number of arguments varies" do
assert first("One") == :__
end
def sum_up(thing) when is_list(thing), do: :entire_list def sum_up(thing) when is_list(thing), do: :entire_list
def sum_up(_thing), do: :single_thing def sum_up(_thing), do: :single_thing
koan "You can 'guard' functions against their arguments" do koan "You can 'guard' functions from their arguments" do
assert sum_up([1,2,3]) == :entire_list assert sum_up([1,2,3]) == :__
assert sum_up(1) == :single_thing end
koan "no guard means just that - anything" do
assert sum_up(1) == :__
end end
def bigger(a,b) when a > b, do: "#{a} is bigger than #{b}" def bigger(a,b) when a > b, do: "#{a} is bigger than #{b}"
def bigger(a,b) when a <= b, do: "#{a} is not bigger than #{b}" def bigger(a,b) when a <= b, do: "#{a} is not bigger than #{b}"
koan "You can also create intricate guards based on the values" do koan "You can also create intricate guards based on the values..." do
assert bigger(10, 5) == "10 is bigger than 5" assert bigger(10, 5) == :__
assert bigger(4, 27) == "4 is not bigger than 27" end
koan "...to make the body of the function clearer" do
assert bigger(4, 27) == :__
end end
def the_length(0), do: "It was zero" def the_length(0), do: "It was zero"
def the_length(number), do: "The length was #{number}" def the_length(number), do: "The length was #{number}"
koan "You can also 'guard' with concrete values" do koan "You can also 'guard' with concrete values" do
assert the_length(0) == "It was zero" assert the_length(0) == :__
assert the_length(5) == "The length was 5" end
koan "Or just let the argument roll" do
assert the_length(5) == :__
end end
koan "You can also define inline functions and call them with .()" do koan "You can also define inline functions and call them with .()" do
multiply = fn (a,b) -> a * b end multiply = fn (a,b) -> a * b end
assert multiply.(2,3) == 6 assert multiply.(2,3) == :__
end end
koan "You can even go shorter, by using &(..) and positional arguments" do koan "You can even go shorter, by using &(..) and positional arguments" do
multiply = &(&1 * &2) multiply = &(&1 * &2)
assert multiply.(2,3) == 6 assert multiply.(2,3) == :__
end end
def two_arguments(_first, second), do: second def two_arguments(_first, second), do: second
koan "You can also show that certain arguments are ignored in the body by adding an underscore" do koan "You can also show that certain arguments are ignored in the body by adding an underscore" do
assert two_arguments(:hi_there, "the other one") == "the other one" assert two_arguments(:hi_there, "the other one") == :__
end end
def multiply_then_call(number, fun), do: fun.(number*5) def multiply_then_call(number, fun), do: fun.(number*5)
def square(number), do: number * number def square(number), do: number * number
koan "You can 'capture' functions if you want to pass them around as values" do koan "You can 'capture' functions if you want to pass them around as values" do
assert multiply_then_call(2, &square/1) == 100 assert multiply_then_call(2, &square/1) == :__
end end
koan "functions can be combined elegantly with the pipe operator" do koan "functions can be combined elegantly with the pipe operator" do
@@ -99,6 +111,6 @@ defmodule Functions do
|> Enum.map(&(String.capitalize(&1))) |> Enum.map(&(String.capitalize(&1)))
|> Enum.join(" ") |> Enum.join(" ")
assert result == "Full Name" assert result == :__
end end
end end

View File

@@ -71,6 +71,32 @@ defmodule KoansHarnessTest do
test_all(Strings, answers) test_all(Strings, answers)
end end
test "Functions" do
answers = [
:light,
3,
:it_was_truthy,
"It really wasn't",
"Hello Hello Hello ",
"Hello Hello Hello Hello Hello ",
"One and Two",
"only One",
:entire_list,
:single_thing,
"10 is bigger than 5",
"4 is not bigger than 27",
"It was zero",
"The length was 5",
6,
6,
"the other one",
100,
"Full Name"
]
test_all(Functions, answers)
end
def test_all(module, answers) do def test_all(module, answers) do
module.all_koans module.all_koans
|> Enum.zip(answers) |> Enum.zip(answers)