Skip to content

Instantly share code, notes, and snippets.

@BennyHallett
Created August 10, 2014 08:37
Show Gist options
  • Save BennyHallett/00ea5440efb7d35a4b18 to your computer and use it in GitHub Desktop.
Save BennyHallett/00ea5440efb7d35a4b18 to your computer and use it in GitHub Desktop.
defmodule FizzBuzz do
@doc """
Given a range, returns an array of values conforming to the FizzBuzz pattern.
## Example
FizzBuzz.calculate 1..15
-> 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
"""
def calculate(range) do
Enum.map(range, &(FizzBuzz.calculate_value &1))
end
@doc """
Calculate the FizzBuzz value for a single number
"""
def calculate_value(value) when rem(value, 15) == 0, do: "FizzBuzz"
def calculate_value(value) when rem(value, 3) == 0, do: "Fizz"
def calculate_value(value) when rem(value, 5) == 0, do: "Buzz"
def calculate_value(value), do: value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment