[英]Can I Avoid Tupled Parameters In This Elixir Anonymous Function?
I'm working through "Programming Elixir" and I came across the exercise headed "Exercise: Functions 2". 我正在通过“编程Elixir”工作,我遇到了题为“练习:功能2”的练习。 Long story short, basically code a function which emits Fizzbuzz if the first two parameters are 0, Fizz if the first param is 0, Buzz if the second param is 0 and the third param if neither of the first two are zero.
长话短说,基本上编码一个函数,如果前两个参数为0则发出Fizzbuzz,如果第一个参数为0则为Fizz,如果第二个参数为0则为Buzz,如果前两个参数均为零则为第三个参数。 I came up with this:
我想出了这个:
fizzbuzztest = fn
{0,0,_} -> "FizzBuzz"
{0,_,_} -> "Fizz"
{_,0,_} -> "Buzz"
{_,_,v} -> "#{v}"
end
Called like so: 这样称呼:
fizzbuzztest.({0,0,8}) # "FizzBuzz"
But I'm wondering--is there some way to do this without having to tuple the parameters? 但我想知道 - 是否有一些方法可以做到这一点,而无需参数元组? It seems there should be some way to pass three arguments and work the pattern match but I haven't found it yet.
似乎应该有一些方法来传递三个参数并处理模式匹配,但我还没有找到它。 Any suggestions from those more experienced with Elixir would be welcome.
欢迎那些对Elixir更有经验的人提出任何建议。
You can solve this particular exercise with: 您可以使用以下方法解决此特定练习
fizzbuzztest = fn
0,0,_ -> "FizzBuzz"
0,_,_ -> "Fizz"
_,0,_ -> "Buzz"
_,_,v -> "#{v}"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.