Improve the function koans slightly
This commit is contained in:
@@ -1,22 +1,20 @@
|
|||||||
defmodule Functions do
|
defmodule Functions do
|
||||||
use Koans
|
use Koans
|
||||||
|
|
||||||
# A function starts with 'def', has a 'do-end' pair
|
def greet(name) do
|
||||||
def inside() do
|
"Hello, #{name}!"
|
||||||
:light
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "It returns the last statement that was called" do
|
koan "Functions take arguments and return values" do
|
||||||
assert inside() == :__
|
assert greet("World") == :__
|
||||||
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,:__) == 6
|
assert quick_inline_product(2,:__) == 6
|
||||||
end
|
end
|
||||||
|
|
||||||
# A function can have an argument between parentheses, after the name
|
|
||||||
def will_change(choice) do
|
def will_change(choice) do
|
||||||
if choice do
|
if choice do
|
||||||
:it_was_truthy
|
:it_was_truthy
|
||||||
@@ -49,7 +47,7 @@ defmodule Functions do
|
|||||||
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 with the same name are distinguished by arity" do
|
koan "Functions with the same name are distinguished by the number of arguments they take" do
|
||||||
assert first("One", "Two") == :__
|
assert first("One", "Two") == :__
|
||||||
assert first("One") == :__
|
assert first("One") == :__
|
||||||
end
|
end
|
||||||
@@ -72,9 +70,11 @@ defmodule Functions do
|
|||||||
|
|
||||||
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) == :__
|
assert the_length(0) == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "Or just let the argument roll" do
|
koan "Or just let the argument roll" do
|
||||||
assert the_length(5) == :__
|
assert the_length(5) == :__
|
||||||
end
|
end
|
||||||
@@ -97,7 +97,7 @@ defmodule Functions do
|
|||||||
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. Mind the ampersand '&' and the slash followed by the number of args." do
|
||||||
assert multiply_then_call(2, &square/1) == :__
|
assert multiply_then_call(2, &square/1) == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ defmodule KoansHarnessTest do
|
|||||||
|
|
||||||
test "Functions" do
|
test "Functions" do
|
||||||
answers = [
|
answers = [
|
||||||
:light,
|
"Hello, World!",
|
||||||
3,
|
3,
|
||||||
{:multiple, [:it_was_truthy, "It really wasn't"]},
|
{:multiple, [:it_was_truthy, "It really wasn't"]},
|
||||||
"Hello Hello Hello ",
|
"Hello Hello Hello ",
|
||||||
|
|||||||
Reference in New Issue
Block a user