-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfizzbuzz.exs
More file actions
33 lines (26 loc) · 915 Bytes
/
fizzbuzz.exs
File metadata and controls
33 lines (26 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
defmodule FizzBuzz do
def upto(final), do: upto_case(final)
def upto_case(final), do: 1..final |> Enum.map(&fizzbuzz_case/1)
def fizzbuzz_case(n) do
case {rem(n, 3), rem(n, 5)} do
{0, 0} -> "FizzBuzz"
{0, _} -> "Fizz"
{_, 0} -> "Buzz"
_ -> n
end
end
def upto_cond(final), do: 1..final |> Enum.map(&fizzbuzz_cond/1)
def fizzbuzz_cond(n) do
cond do
rem(n, 3) == 0 and rem(n, 5) == 0 -> "FizzBuzz"
rem(n, 3) == 0 -> "Fizz"
rem(n, 5) == 0 -> "Buzz"
true -> n
end
end
def upto_guard(final), do: 1..final |> Enum.map(&fizzbuzz_guard/1)
def fizzbuzz_guard(n) when rem(n, 3) == 0 and rem(n, 5) == 0, do: "FizzBuzz"
def fizzbuzz_guard(n) when rem(n, 3) == 0, do: "Fizz"
def fizzbuzz_guard(n) when rem(n, 5) == 0, do: "Fizz"
def fizzbuzz_guard(n), do: n
end