Last active
September 20, 2017 14:45
-
-
Save seanhandley/74a1fae8eba1ff0af472b34f0a918218 to your computer and use it in GitHub Desktop.
FizzBuzz in Elixir / The Magic of Functional Pattern Matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fb = fn | |
0, 0, _ -> "FizzBuzz" | |
0, _, _ -> "Fizz" | |
_, 0, _ -> "Buzz" | |
_, _, x -> x | |
end | |
fizzbuzz = fn (n) -> fb.(rem(n, 3), rem(n, 5), n) end | |
fizzbuzz.(10) # => "Buzz" | |
fizzbuzz.(11) # => 11 | |
fizzbuzz.(12) # => "Fizz" | |
fizzbuzz.(13) # => 13 | |
fizzbuzz.(14) # => 14 | |
fizzbuzz.(15) # => "FizzBuzz" | |
fizzbuzz.(16) # => 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment