Created
August 10, 2014 08:37
-
-
Save BennyHallett/00ea5440efb7d35a4b18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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