small medium large xlarge

• The operator `rem(a, b)` returns the remainder after dividing `a` by `b`. Write a function that takes a single integer (`n`) calls the function in the previous exercise, passing it `rem(n,3)`, `rem(n,5)`, and `n`. Call it 7 times with the arguments 10, 11, 12, etc. You should get “Buzz, 11, Fizz, 13, 14, FizzBuzz”, 16”.

(Yes, it’s a FizzBuzz1 solution with no conditional logic).

A Possible Solution</summary>

```iex(3)> fb = fn n ->
...(3)>   fizz_word.(rem(n, 3), rem(n, 5), n)
...(3)>   end
#Function<erl_eval.6.17052888>

iex(4)> [ fb.(10), fb.(11), fb.(12), fb.(13), fb.(14), fb.(15), fb.(16) ]
["Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16]
```

</details>

1. http://c2.com/cgi/wiki?FizzBuzzTest

This doesn’t quite meet the exact requirements (it uses concepts not yet introduced by that point in the book and covers more than 7 numbers) but who cares!

``````defmodule FizzBuzz do
def  transform_range(r), do: Enum.map(r, &transform_value/1)
defp transform_value(n), do: transform(rem(n, 3), rem(n, 5), n)
defp transform(0, 0, _), do: "FizzBuzz"
defp transform(0, _, _), do: "Fizz"
defp transform(_, 0, _), do: "Buzz"
defp transform(_, _, n), do: n
end

# Map a range of numbers (1 - 100) to their "fizzbuzz values."
fizzbuzz_values = FizzBuzz.transform_range 1..100

# Print each value on a new line.
Enum.each fizzbuzz_values, &IO.puts/1
``````
``````func = fn
(0, 0, _) -> "FizzBuzz"
(0, _, _) -> "Fizz"
(_, 0, _) -> "Buzz"
(_, _, a) -> a
end

func_2 = fn
(n) -> func.(rem(n, 3), rem(n, 5), n)
|> IO.puts
end

func_2.(10)
func_2.(11)
func_2.(12)
func_2.(13)
func_2.(14)
func_2.(15)
func_2.(16)
``````
``````fizz_buzz = fn
{0, 0, _} -> "FizzBuzz"
{0, _, _} -> "Fizz"
{_, 0, _} -> "Buzz"
{_, _, c} -> c
end

number_to_eval = fn
n -> fizz_buzz.({rem(n, 3), rem(n, 5), n})
end

IO.puts number_to_eval.(10)
IO.puts number_to_eval.(11)
IO.puts number_to_eval.(12)
IO.puts number_to_eval.(13)
IO.puts number_to_eval.(14)
IO.puts number_to_eval.(15)
IO.puts number_to_eval.(16)
``````
``````buzzword = fn
0, 0, _ -> "FizzBuzz"
0, _, _ -> "Fizz"
_, 0, _ -> "Buzz"
_, _, n -> n |> to_string
end

buzzer = fn n -> buzzword.(rem(n, 3), rem(n, 5), n) end

fizzbuzz = fn range -> Enum.map_join(range, ", ", buzzer) <> "." end

fizzbuzz.(10..16) # => "Buzz, 11, Fizz, 13, 14, FizzBuzz, 16."
``````
`````` fizzbuzz = fn
0, 0, _ -> "FizzBuzz"
0, _, _ -> "Fizz"
_, 0, _ -> "Buzz"
_, _, n -> n
end

remm = fn (n) -> fizbuzz.(rem(n,3),rem(n,5),n) end

Enum.each(10..20, fn(x)->IO.puts remm.(x) end)
``````

The “no conditional” function:

``````word = fn
0, 0, _ -> "FizzBuzz"
0, _, _ -> "Fizz"
_, 0, _ -> "Buzz"
_, _, res -> res
end
``````

Then, using a `main` function to employ `rem/2`, I made seven function calls, but then dug around and figured out how to use `Enum.each` on a `Range`:

``````main = fn
n -> word.(rem(n, 3), rem(n, 5), n)
end

Enum.each(10..16, fn(x) -> IO.puts(main.(x)) end)
``````

That seemed like it could be improved upon, so once I figure out that `each` will take a reference to a function, I changed it into:

``````main = fn
n -> IO.puts(word.(rem(n, 3), rem(n, 5), n))
end

Enum.each(10..16, main)
``````

Still, this displayed the results one under each other, rather than in one line, so borrowing from Ruby I guessed that where there’s an `each` there should be a `map`, and lo and behold, there is (I also had to steal `IO.inspect` because I couldn’t figure out how to do a join):

``````main = fn
n -> word.(rem(n, 3), rem(n, 5), n)
end

IO.inspect Enum.map(10..16, main)
``````

OK, now I’m hooked, hard! Coming from 10 years of Ruby this really makes me smile. It feels like when I first learned Ruby, but with more “you gotta be kidding me!”, and “that is freaking awesome!” moments.

Thanks Dave, and Jose!

I feel the fun is just beginning…

``````fb = fn
0, 0, _ -> "FizzBuzz"
0, _, _ -> "Fizz"
_, 0, _ -> "Buzz"
_, _, a -> a
end

my_rem = fn n -> fb.(rem(n,3), rem(n,5), n) end

IO.puts my_rem.(10)
IO.puts my_rem.(11)
IO.puts my_rem.(12)
IO.puts my_rem.(13)
IO.puts my_rem.(14)
IO.puts my_rem.(15)
IO.puts my_rem.(16)

``````

I came up with the following. What was interesting was I originally had the (fn) definitions in reverse order and the compile failed. Lesson: compilation is purely sequential and there’s no first pass for lexical scoping.

``````fizz_buzz = fn
0,0,_ -> "FizzBuss"
0,_,_ -> "Fizz"
_,0,_ -> "Buzz"
_,_,x -> x
end

fb2 = fn
x -> fizz_buzz.(rem(x,3), rem(x,5), x )
end

IO.puts fb2.(10)
IO.puts fb2.(11)
IO.puts fb2.(12)
IO.puts fb2.(13)
IO.puts fb2.(14)
IO.puts fb2.(15)
IO.puts fb2.(16)
IO.puts fb2.(17)

``````

It looks good how simple you can write the code, awesome so far. :)

``````buzz_fn = fn
0, 0, _ -> "FizzBuzz"
0, _, _ -> "Fizz"
_, 0, _ -> "Buzz"
_, _, c -> c
end

buzz_with_rem_fn = fn n -> buzz_fn.(rem(n, 3), rem(n, 5), n) end

IO.inspect Enum.map_join(10..16, ", ", buzz_with_rem_fn) <> "."
``````

http://c2.com/cgi/wiki?FizzBuzzTest

Not sure if this was always the case, but I have been getting the 404 for two days.

``````
goru = fn
0,0,_ -> IO.puts("FizzBuzz")
0,_,_ -> IO.puts("Fizz")
_,0,_ -> IO.puts("Buzz")
_,_,c -> IO.puts(c)

end

dad = fn n -> goru.(rem(n,3),rem(n,5),n) end

``````
``````fun_buzz = fn
0,0,_ ->  IO.puts "FizzBuzz."
0,_,_ -> IO.puts "Fizz."
_,_, c -> IO.puts c
end

IO.puts fun_buzz.(0,0,4)
IO.puts fun_buzz.(0,3,4)
IO.puts fun_buzz.(4,4,4)

fun_rem = fn
n ->  fun_buzz.(rem(n,3), rem(n,5), n)
end

IO.puts "---- ------- -----------"

fun_rem.(10)
fun_rem.(11)
fun_rem.(12)
fun_rem.(13)
fun_rem.(14)
fun_rem.(15)
fun_rem.(16)

``````
You must be logged in to comment