I'm trying to solve the classic fizzbuzz problem using Elixir. I found a few different ways to solve this but the best way was this:
fizzbuzz = fn
(0, 0, _) -> "FizzBuzz"
(0, _, _) -> "Fizz"
(_, 0, _) -> "Buzz"
(_, _, a) -> a
end
fb = fn n -> fizzbuzz.(rem(n, 3), rem(n, 5), n) end
fb.(10)
My problem now is that I want to call the fb
anonymous function 100 times. In ruby it would look like this:
100.times do |i|
fb.(i)
end
Obviously, that wouldn't work because you can't call an anonymous function like that in Ruby. But I hope you get the picture. How can I achieve this in Elixir?
(1 .. 100) |> Enum.each(fb)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With