Elixir is a beautiful language. Among its strength is its powerful pattern matching system which allows one to write very declarative, elegant code. FizzBuzz is a trivial problem often presented to new programmers or interviewees to determine baseline programming ability. Here is a solution Elixir:
defmodule FizzBuzz do
def fizzbuzz(n) do
1 .. n
|> Stream.map(&calc/1)
|> Enum.each(&IO.puts/1)
end
defp calc(i) when rem(i, 15) == 0, do: "FizzBuzz"
defp calc(i) when rem(i, 3) == 0, do: "Fizz"
defp calc(i) when rem(i, 5) == 0, do: "Buzz"
defp calc(i), do: i
endThis kind of declarative syntax frequently makes handling changes in business requirements as easy as adding an additional definition for a function.
Version that doesn't repeat the 3 and 5 calculations for 15: