Can I avoid alternating parameters in this anonymous function?

I am working on Elixir Programming, and I came across an exercise called Exercise: Features 2. In short, basically the code of the function that emits Fizzbuzz if the first two parameters are 0, Fizz if the first parameter is 0, Buzz if the second parameter is 0, and the third parameter if none of the first two is zero. I came up with this:

fizzbuzztest = fn
   {0,0,_} -> "FizzBuzz"
   {0,_,_} -> "Fizz"
   {_,0,_} -> "Buzz"
   {_,_,v} -> "#{v}"
end

Called like this:

fizzbuzztest.({0,0,8}) # "FizzBuzz"

But I am wondering - is there a way to do this without loading the parameters? There seems to be some way to pass three arguments and handle pattern matching, but I haven't found it yet. Any suggestions from those who are more experienced with Elixir would be welcome.

+3
source share
1 answer

You can solve this exercise with:

fizzbuzztest = fn
   0,0,_ -> "FizzBuzz"
   0,_,_ -> "Fizz"
   _,0,_ -> "Buzz"
   _,_,v -> "#{v}"
end
+5
source

All Articles