DEV Community

Discussion on: First Impressions of Elixir

Collapse
 
jackmarchant profile image
Jack Marchant

Hey Anton, I hadn't tried a FizzBuzz in Elixir yet, so I took a stab at it:

defmodule FizzBuzz do
  def run(n) when is_integer(n), do: do_fizzbuzz(n)

  defguardp divisible_by_3(n) when rem(n, 3) === 0
  defguardp divisible_by_5(n) when rem(n, 5) === 0

  defp do_fizzbuzz(n) when divisible_by_3(n) and divisible_by_5(n), do: "FizzBuzz"
  defp do_fizzbuzz(n) when divisible_by_3(n), do: "Fizz"
  defp do_fizzbuzz(n) when divisible_by_5(n), do: "Buzz"
end
Collapse
 
antonrich profile image
Anton

Interesting : )