Created
December 5, 2013 02:10
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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