From 61850d07ff3c4e9efbf99739851cf58d51433917 Mon Sep 17 00:00:00 2001 From: Shannon Oram Date: Sun, 8 Jul 2018 12:18:30 +1000 Subject: [PATCH] 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 c20a478..18a0c66 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