Skip to content

Instantly share code, notes, and snippets.

@duck57
Created October 29, 2021 22:24
Show Gist options
  • Save duck57/d5fd843885c80c33f54278c4700e5758 to your computer and use it in GitHub Desktop.
Save duck57/d5fd843885c80c33f54278c4700e5758 to your computer and use it in GitHub Desktop.
A more generic FizzBuzz implementation
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