Initial work on Functions

This commit is contained in:
Felipe Sere
2016-03-20 20:21:22 +00:00
parent 23b08a6c81
commit 679bacc632
2 changed files with 16 additions and 24 deletions

View File

@@ -1,15 +1,17 @@
defmodule Functions do defmodule Functions do
use Koans use Koans
# A function starts with 'def', has a 'do-end' pair
def inside() do def inside() do
:light :light
end end
# 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() == :__ 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,:__) == 6 assert quick_inline_product(2,:__) == 6
end end
@@ -23,11 +25,8 @@ defmodule Functions do
end end
end end
koan "Can return atoms..." do koan "Functions can return different things" do
assert will_change(true) == :__ assert will_change(true) == :__
end
koan "..or strings from the same function!" do
assert will_change(false) == :__ assert will_change(false) == :__
end end
@@ -49,29 +48,25 @@ 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 arity" do
assert first("One", "Two") == :__ assert first("One", "Two") == :__
end
koan "the name stays - the number of arguments varies" do
assert first("One") == :__ assert first("One") == :__
end 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 "You can 'guard' functions from their arguments" do
assert sum_up([1,2,3]) == :__ assert sum_up([1,2,3]) == :__
end
koan "no guard means just that - anything" do
assert sum_up(1) == :__ 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) == :__ assert bigger(10, 5) == :__
end
koan "...to make the body of the function clearer" do
assert bigger(4, 27) == :__ assert bigger(4, 27) == :__
end end
@@ -101,11 +96,12 @@ 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" do
assert multiply_then_call(2, &square/1) == :__ 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
result = "full-name" result = "full-name"
|> String.split("-") |> String.split("-")
|> Enum.map(&(String.capitalize(&1))) |> Enum.map(&(String.capitalize(&1)))

View File

@@ -144,16 +144,12 @@ defmodule KoansHarnessTest do
answers = [ answers = [
:light, :light,
3, 3,
:it_was_truthy, {:multiple, [:it_was_truthy, "It really wasn't"]},
"It really wasn't",
"Hello Hello Hello ", "Hello Hello Hello ",
"Hello Hello Hello Hello Hello ", "Hello Hello Hello Hello Hello ",
"One and Two", {:multiple, ["One and Two", "only One"]},
"only One", {:multiple, [:entire_list, :single_thing]},
:entire_list, {:multiple, ["10 is bigger than 5", "4 is not bigger than 27"]},
:single_thing,
"10 is bigger than 5",
"4 is not bigger than 27",
"It was zero", "It was zero",
"The length was 5", "The length was 5",
6, 6,