Fizzbuzz in Elixir

Warning: This post is very old. It may be out of date, and links in it may not work. Continue at your own risk.

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
    |> Enum.each(&IO.puts/1)

  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)

This kind of declarative syntax frequently makes handling changes in business requirements as easy as adding an additional definition for a function.

I provide technical leadership, risk assessment, project planning, training, and additional development to software teams. If you need help taking a project from idea through execution, get in touch.