Skip to content

Instantly share code, notes, and snippets.

@Dania02525
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save Dania02525/3559821dffbc7d88d46c to your computer and use it in GitHub Desktop.

Select an option

Save Dania02525/3559821dffbc7d88d46c to your computer and use it in GitHub Desktop.
Elixir fizzbuzz pattern matching
defmodule Fizzbuzz do
def fizzbuzzer(n) do
case {rem(n, 3), rem(n, 5), n} do
{0, 0, _} ->
"FizzBuzz"
{0, _, _} ->
"Fizz"
{_, 0, _} ->
"Buzz"
{_, _, n} ->
n
end
end
def fizzbuzz(range) do
if Range.range?(range) do
range
|> Enum.map(fn(n) -> fizzbuzzer(n) end)
|> Enum.each(fn(x) -> IO.puts(x) end)
else
IO.puts("first parameter must be a range!")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment