Skip to content

Commit

Permalink
Add koan "You can use pattern matching to define [...]"
Browse files Browse the repository at this point in the history
Koan added following comment on Github.
elixirkoans#222 (comment)

[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]
  • Loading branch information
s-oram committed Jul 11, 2018
1 parent ecb1301 commit 678d15d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/koans/13_functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 678d15d

Please sign in to comment.