纯函数的输入列表
纯函数的语法类似于 (1+#1+#2)&[a,b]
,它给出 1+a+b
。现在我想将一些类似于 {a,b}
的函数的输出提供给上面的函数,即类似 (1+#1+#2)&{a ,b}
,但是使用正确的语法,因为这显然不起作用。我该怎么做呢?
The syntax for a pure function is something like (1+#1+#2)&[a,b]
, which gives 1+a+b
. Now I want to supply the output from some function which looks like {a,b}
to the function above, i.e., something like (1+#1+#2)&{a,b}
, but with the correct syntax, as that obviously doesn't work. How do I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是使用
Apply
(@@
):The easiest approach is to use
Apply
(@@
):要提供一些替代方案,如果更方便的话,您还可以在函数中包含
Apply
:或者,您可以手动索引部件:
最后,您可能已经知道这一点,但对于阅读此问题的其他人来说:
To provide some alternatives, you can also include the
Apply
within the function if that is more convenient:Optionally, you can index parts manually:
Finally, you may already know this, but for others reading this question:
这是一个普通函数的版本(即可以使用方括号),它将采用任意列表。
Apply
已移至函数内部,##
表示SlotSequence
(参见_
和__
在模式匹配中)Here's a version that is an ordinary function (ie can use square brackets) that will take an arbitrary list. The
Apply
has been moved inside the function and the##
meansSlotSequence
(c.f._
and__
in pattern matching)