Created
October 29, 2021 22:24
-
-
Save duck57/d5fd843885c80c33f54278c4700e5758 to your computer and use it in GitHub Desktop.
A more generic FizzBuzz implementation
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 | |
def fizzbuzz(words \\ [{3, "Fizz"}, {5, "Buzz"}], min \\ 1, max \\ 100) do | |
min..max |> Enum.map(&_fizznum(&1, words)) | |
end | |
def _fizznum(n, words) do | |
w = words |> Enum.map(&_checkword(&1, n)) |> Enum.join("") | |
case w do | |
"" -> n | |
_ -> w | |
end | |
end | |
def _checkword({div, word}, n) when rem(n, div) == 0, do: word | |
def _checkword(_, _n), do: "" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment