Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an anonymous function 100 times in Elixir

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?

like image 985
Bitwise Avatar asked Dec 01 '22 11:12

Bitwise


1 Answers

(1 .. 100) |> Enum.each(fb)
like image 85
Mike Buhot Avatar answered Dec 19 '22 09:12

Mike Buhot