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]
This commit is contained in:
Shannon Oram
2018-07-08 12:18:30 +10:00
parent ecb130165e
commit 678d15de07

View File

@@ -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