Improve the wording in the function koans

This commit is contained in:
Felipe Sere
2016-04-10 00:19:26 +01:00
parent 0bd5ce772c
commit 2b999605f5
2 changed files with 24 additions and 57 deletions

View File

@@ -5,57 +5,36 @@ defmodule Functions do
"Hello, #{name}!" "Hello, #{name}!"
end end
koan "Functions take arguments and return values" do koan "Functions map arguments to outputs" do
assert greet("World") == :__ assert greet("World") == :__
end end
def quick_inline_product(a, b), do: a * b def multiply(a, b), do: a * b
koan "Single line functions are cool, but mind the command and the colon!" do
koan "Short functions can be defined in a single line, but mind the comman and colon" do assert multiply(2, :__) == 6
assert quick_inline_product(2,:__) == 6
end
def will_change(choice) do
if choice do
:it_was_truthy
else
"It really wasn't"
end
end
koan "Functions can return different things" do
assert will_change(true) == :__
assert will_change(false) == :__
end
def repeat(times, message) do
String.duplicate(message, times)
end
koan "Functions can have more than one arguement" do
assert repeat(3, "Hello ") == :__
end
def repeat_again(times \\ 5, message) do
String.duplicate(message, times)
end
koan "But sometimes, you may want to default some arguments" do
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 with the same name are distinguished by the number of arguments they take" 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
def repeat_again(message, times \\ 5) do
String.duplicate(message, times)
end
koan "Not all arguments are always needed" do
assert repeat_again("Hello ") == :__
assert repeat_again("Hello ", 2) == :__
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 from their arguments" do koan "Functions can be picky and apply to only certain types" do
assert sum_up([1,2,3]) == :__ assert sum_up([1,2,3]) == :__
assert sum_up(1) == :__ assert sum_up(1) == :__
end end
@@ -63,7 +42,7 @@ defmodule Functions do
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 "Intricate guards are possible, but be mindful of the reader" do
assert bigger(10, 5) == :__ assert bigger(10, 5) == :__
assert bigger(4, 27) == :__ assert bigger(4, 27) == :__
end end
@@ -71,15 +50,12 @@ 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 "For those individual one-offs, you can even guard on the arguments themselves" do
assert the_length(0) == :__ assert the_length(0) == :__
end
koan "Or just let the argument roll" do
assert the_length(5) == :__ assert the_length(5) == :__
end end
koan "You can also define inline functions and call them with .()" do koan "Little anonymous functions are common, and called with a dot" do
multiply = fn (a,b) -> a * b end multiply = fn (a,b) -> a * b end
assert multiply.(2,3) == :__ assert multiply.(2,3) == :__
end end
@@ -89,16 +65,11 @@ defmodule Functions do
assert multiply.(2,3) == :__ assert multiply.(2,3) == :__
end end
def two_arguments(_first, second), do: second def times_five_and_then(number, fun), do: fun.(number*5)
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") == :__
end
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. Mind the ampersand '&' and the slash followed by the number of args" do koan "You can pass functions around as arguments. Place and '&' before the name and state the arity" do
assert multiply_then_call(2, &square/1) == :__ assert times_five_and_then(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

View File

@@ -138,17 +138,13 @@ defmodule KoansHarnessTest do
answers = [ answers = [
"Hello, World!", "Hello, World!",
3, 3,
{:multiple, [:it_was_truthy, "It really wasn't"]}, {:multiple, ["One and Two", "Only One"]},
"Hello Hello Hello ", {:multiple, ["Hello Hello Hello Hello Hello ","Hello Hello "]},
"Hello Hello Hello Hello Hello ",
{:multiple, ["One and Two", "only One"]},
{:multiple, [:entire_list, :single_thing]}, {:multiple, [:entire_list, :single_thing]},
{:multiple, ["10 is bigger than 5", "4 is not bigger than 27"]}, {:multiple, ["10 is bigger than 5", "4 is not bigger than 27"]},
"It was zero", {:multiple, ["It was zero", "The length was 5"]},
"The length was 5",
6, 6,
6, 6,
"the other one",
100, 100,
"Full Name" "Full Name"
] ]