From 678d15de0722f84b2b218a5c9087cd26b6f76c03 Mon Sep 17 00:00:00 2001 From: Shannon Oram Date: Sun, 8 Jul 2018 12:18:30 +1000 Subject: [PATCH 1/3] Add koan "You can use pattern matching to define [...]" Koan added following comment on Github. https://github.com/elixirkoans/elixir-koans/issues/222#issuecomment-399979891 [quote] And, in fact, you can define multiple cases for a function using this sytnax: ``` lolwat = fn "lol" -> "wat" _ -> "haha" end lolwat.("lol") # => "wat" lolwat.("anything") # => "haha" lolwat.("rly") # => "haha" ``` [/quote] --- lib/koans/13_functions.ex | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/koans/13_functions.ex b/lib/koans/13_functions.ex index c20a4785..18a0c66b 100644 --- a/lib/koans/13_functions.ex +++ b/lib/koans/13_functions.ex @@ -78,6 +78,16 @@ defmodule Functions do assert three_times.("foo") == ___ end + koan "You can use pattern matching to define multiple cases for anonymous functions" do + format_result = fn + {:ok, result} -> "Success is #{result}" + {:error, reason} -> "You just lost #{reason}" + end + + assert format_result.({:ok, "no accident"}) == ___ + assert format_result.({:error, "the game"}) == ___ + end + def times_five_and_then(number, fun), do: fun.(number * 5) def square(number), do: number * number From 362cbf77c008d08eebdb05e553ebac95b28a592e Mon Sep 17 00:00:00 2001 From: Shannon Oram Date: Sun, 8 Jul 2018 12:18:30 +1000 Subject: [PATCH 2/3] Rename function. format_result() -> inspirational_quote() --- lib/koans/13_functions.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/koans/13_functions.ex b/lib/koans/13_functions.ex index 18a0c66b..add576b0 100644 --- a/lib/koans/13_functions.ex +++ b/lib/koans/13_functions.ex @@ -79,13 +79,13 @@ defmodule Functions do end koan "You can use pattern matching to define multiple cases for anonymous functions" do - format_result = fn + inspirational_quote = fn {:ok, result} -> "Success is #{result}" {:error, reason} -> "You just lost #{reason}" end - assert format_result.({:ok, "no accident"}) == ___ - assert format_result.({:error, "the game"}) == ___ + assert inspirational_quote.({:ok, "no accident"}) == ___ + assert inspirational_quote.({:error, "the game"}) == ___ end def times_five_and_then(number, fun), do: fun.(number * 5) From 38cbc98f81fc1ae66b9b472240575155c7600aad Mon Sep 17 00:00:00 2001 From: Shannon Oram Date: Thu, 12 Jul 2018 10:36:12 +1000 Subject: [PATCH 3/3] Add test for new koan --- test/koans/functions_koans_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/koans/functions_koans_test.exs b/test/koans/functions_koans_test.exs index 78b47d1a..eb48b502 100644 --- a/test/koans/functions_koans_test.exs +++ b/test/koans/functions_koans_test.exs @@ -15,6 +15,7 @@ defmodule FunctionsTests do 6, "Hi, Foo!", ["foo", "foo", "foo"], + {:multiple, ["Success is no accident", "You just lost the game"]}, 100, 1000, "Full Name",