Skip to content

Instantly share code, notes, and snippets.

@mutablestate
Created July 29, 2014 02:59
Show Gist options
  • Select an option

  • Save mutablestate/86193c677213ddb528b3 to your computer and use it in GitHub Desktop.

Select an option

Save mutablestate/86193c677213ddb528b3 to your computer and use it in GitHub Desktop.
FizzBuzz implementation in Elixir using cond
defmodule ElixirBuzz do
@moduledoc "It implements a fizzbuzz solution with cond"
@doc """
Returns a range from 1 to the given number in as list substituting a number for a pre-defined string when divisible by 3, 5, or 15 (3 and 5)
"""
def buzz(number) do
Enum.map(1..number, fn n ->
cond do
rem(n, 15) == 0 -> "ElixirBuzz"
rem(n, 3) == 0 -> "Elixir"
rem(n, 5) == 0 -> "Buzz"
true -> n
end
end)
end
end
@doc """
iex> c "elixir_buzz.exs"
iex> ElixirBuzz.buzz(50)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment