在 Clojure 中,我如何将所有内容映射到常量值?
例如
(map #(+ 10 %1) [ 1 3 5 7 ])
将向所有内容添加 10
假设我想将所有内容映射到常量 1。我已经尝试过
(map #(1) [ 1 3 5 7 ])
但我不明白编译器错误。
For example
(map #(+ 10 %1) [ 1 3 5 7 ])
Will add 10 to everything
Suppose I want to map everything to the constant 1. I have tried
(map #(1) [ 1 3 5 7 ])
But I don't understand the compiler error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不起作用有两个原因:
#(1)
是一个零参数匿名函数,因此它不能与 map 一起使用(与一个输入序列一起使用时需要一个单参数函数) 。(1)
- 尝试(#(1))
例如,如果您想查看此错误。以下是一些可行的替代方案:
在上述版本中,我认为使用 constantly 或for 应该是首选 - 这些更清晰、更惯用。
Won't work for two reasons:
#(1)
is a zero-argument anonymous function, so it won't work with map (which requires a one-argument function when used with one input sequence).(1)
- try(#(1))
for example if you want to see this error.Here are some alternatives that will work:
Of the above, I think the versions using constantly or for should be preferred - these are clearer and more idiomatic.
匿名函数
#(+ 10 %1)
相当于:而
#(1)
相当于:并尝试将
1
调用为没有参数的函数将无法工作。The anonymous function
#(+ 10 %1)
is equivalent to:Whereas
#(1)
is equivalent to:And trying to call
1
as a function with no args just won't work.我从 clojure.org 得到这个
通过谷歌搜索“clojure常量函数”这个词,因为我刚刚开始关注clojure
欢呼
I got this from clojure.org
by googling the words "clojure constant function" as I am just beginning to look at clojure
cheers