From 679bacc6329435f66924d42ec210f1f5bf912824 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Sun, 20 Mar 2016 20:21:22 +0000 Subject: [PATCH] Initial work on Functions --- lib/koans/09_functions.ex | 28 ++++++++++++---------------- test/koans_harness_test.exs | 12 ++++-------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/koans/09_functions.ex b/lib/koans/09_functions.ex index 5ab2c7c..151eccd 100644 --- a/lib/koans/09_functions.ex +++ b/lib/koans/09_functions.ex @@ -1,15 +1,17 @@ defmodule Functions do use Koans + # A function starts with 'def', has a 'do-end' pair def inside() do :light 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() == :__ end 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 assert quick_inline_product(2,:__) == 6 end @@ -23,11 +25,8 @@ defmodule Functions do end end - koan "Can return atoms..." do + koan "Functions can return different things" do assert will_change(true) == :__ - end - - koan "..or strings from the same function!" do assert will_change(false) == :__ end @@ -49,29 +48,25 @@ defmodule Functions do def first(foo, bar), do: "#{foo} and #{bar}" 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") == :__ - 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), do: :single_thing + koan "You can 'guard' functions from their arguments" do assert sum_up([1,2,3]) == :__ - end - koan "no guard means just that - anything" do assert sum_up(1) == :__ end 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}" - 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) == :__ - end - koan "...to make the body of the function clearer" do assert bigger(4, 27) == :__ end @@ -101,11 +96,12 @@ defmodule Functions do def multiply_then_call(number, fun), do: fun.(number*5) def square(number), do: number * number + koan "You can 'capture' functions if you want to pass them around as values" do assert multiply_then_call(2, &square/1) == :__ 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" |> String.split("-") |> Enum.map(&(String.capitalize(&1))) diff --git a/test/koans_harness_test.exs b/test/koans_harness_test.exs index 0199d07..7952b71 100644 --- a/test/koans_harness_test.exs +++ b/test/koans_harness_test.exs @@ -144,16 +144,12 @@ defmodule KoansHarnessTest do answers = [ :light, 3, - :it_was_truthy, - "It really wasn't", + {:multiple, [: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", + {:multiple, ["One and Two", "only One"]}, + {:multiple, [:entire_list, :single_thing]}, + {:multiple, ["10 is bigger than 5", "4 is not bigger than 27"]}, "It was zero", "The length was 5", 6,