DEV Community

Discussion on: Fizz Buzz in Every Language

Collapse
 
nielsbom profile image
Niels Bom

Elixir

1..100
|> Enum.map(fn
  n when rem(n, 3) == 0 and rem(n, 5) == 0 -> "FizzBuzz"
  n when rem(n, 3) == 0 -> "Fizz"
  n when rem(n, 5) == 0 -> "Buzz"
  n -> Integer.to_string(n)
end)
|> Enum.each(&IO.puts/1)
Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Ooh, this would make a nice addition to the repo.

Collapse
 
nielsbom profile image
Niels Bom

Go ahead.

Collapse
 
bjorngrunde profile image
Björn Grunde

Here is another example using as a Module and using pattern matching. It looks hilarious :P

defmodule FizzBuzz do

  def run(num) when num >= 1 and num <= 100 do:
    fizzbuzz(num, rem(n, 3), rem(n, 5))
    run(num - 1)
  end

  def run(0), do: {:ok, "Done"}
  def run(_) do: {:error, "Something went wrong"}

  defp fizzbuzz(_, 0, 0), do: IO.puts "Fizzbuzz"
  defp fizzbuzz(_, 0, _), do: IO.puts "Fizz"
  defp fizzbuzz(_, _, 0), do: IO.puts "Buzz"
  defp fizzbuzz(n, _, _), do: n |> to_string |> IO.puts
end

# Example
FizzBuzz.run(55)