Fizzbuzz in Elixir
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 fizz do (1..100) |> Enum.map(&buzz/1) |> Enum.each(&IO.puts/1) end defp buzz(i) when rem(i, 15) == 0, do: "FizzBuzz" defp buzz(i) when rem(i, 5) == 0, do: "Buzz" defp buzz(i) when rem(i, 3) == 0, do: "Fizz" defp buzz(i), do: Integer.to_string(i) end
This kind of declarative syntax frequently makes handling changes in business requirements as easy as adding an additional definition for a function.