Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule FizzbuzzPatt do
def run(number) when rem(number, 15) == 0 do
"FizzBuzz"
end
def run(number) when rem(number, 5) == 0 do
"Buzz"
end
def run(number) when rem(number, 3) == 0 do
"Fizz"
end
def run(number) do
number
end
def run_again(number) when rem(number, 15) == 0, do: "FizzBuzz"
def run_again(number) when rem(number, 5) == 0, do: "Buzz"
def run_again(number) when rem(number, 3) == 0, do: "Fizz"
def run_again(number), do: number
def run_rec([]), do: []
def run_rec(number) when is_integer(number) and number > 0 do
run_rec(Enum.to_list(0..number))
end
def run_rec([head|tail]) do
output(rem(head, 3), rem(head, 5), head)
run_rec(tail)
end
defp output(0,0,n), do: IO.puts "FizzBuzz, #{n}"
defp output(0,_,n), do: IO.puts "Fizz, #{n}"
defp output(_,0,n), do: IO.puts "Buzz, #{n}"
defp output(_,_,n), do: IO.puts "#{n}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment