Skip to content

Instantly share code, notes, and snippets.

@chris
Created December 5, 2013 02:10
Show Gist options
  • Save chris/7799064 to your computer and use it in GitHub Desktop.
Save chris/7799064 to your computer and use it in GitHub Desktop.
FizzBuzz in Elixir (one of the many ways to do it). From the PragProg book.
defmodule FizzBuzz do
def upto(n) when n > 0, do: 1..n |> Enum.map(&fizzbuzz/1)
defp fizzbuzz(n) when rem(n,3) == 0 and rem(n,5) == 0, do: "FizzBuzz"
defp fizzbuzz(n) when rem(n,3) == 0, do: "Fizz"
defp fizzbuzz(n) when rem(n,5) == 0, do: "Buzz"
defp fizzbuzz(n), do: n
end
FizzBuzz.upto(100) |> Enum.join(" ") |> IO.puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment