Skip to content

Instantly share code, notes, and snippets.

@Shemeikka
Created October 30, 2019 17:49
Show Gist options
  • Save Shemeikka/688627338f704d99619c723822b1f6d4 to your computer and use it in GitHub Desktop.
Save Shemeikka/688627338f704d99619c723822b1f6d4 to your computer and use it in GitHub Desktop.
Simple FizzBuzz example using Elixir
defmodule FizzBuzz do
defguard is_fizz(value) when is_integer(value) and rem(value, 3) == 0
defguard is_buzz(value) when is_integer(value) and rem(value, 5) == 0
def run(val) do
1..val
|> Enum.each(fn val -> word(val) |> IO.inspect() end)
end
defp word(val) when is_fizz(val) and is_buzz(val), do: "fizzbuzz"
defp word(val) when is_fizz(val), do: "fizz"
defp word(val) when is_buzz(val), do: "buzz"
defp word(val), do: val
end
FizzBuzz.run(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment