Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Last active September 20, 2017 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhandley/74a1fae8eba1ff0af472b34f0a918218 to your computer and use it in GitHub Desktop.
Save seanhandley/74a1fae8eba1ff0af472b34f0a918218 to your computer and use it in GitHub Desktop.
FizzBuzz in Elixir / The Magic of Functional Pattern Matching
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