clojure 中的匿名函数需要多少个参数?

发布于 2024-12-11 11:25:41 字数 223 浏览 0 评论 0原文

Clojure 如何确定匿名函数(使用 #... 符号创建)需要多少个参数?

user=> (#(identity [2]) 14)
java.lang.IllegalArgumentException: Wrong number of args (1) passed to: user$eval3745$fn (NO_SOURCE_FILE:0)

How does Clojure determine how many arguments an anonymous function (created with the #... notation) expect?

user=> (#(identity [2]) 14)
java.lang.IllegalArgumentException: Wrong number of args (1) passed to: user$eval3745$fn (NO_SOURCE_FILE:0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-12-18 11:25:41

#(println "你好,世界!") ->无参数

#(println (str "Hello, " % "!")) -> 1 个参数(%%1 的同义词)

#(println (str %1 ", " %2 "!")) - > 2 论证

等等。请注意,您不必使用所有 %n,预期的参数数量由最高的 n 定义。因此#(println (str "Hello, " %2)) 仍然需要两个参数。

您还可以使用 %& 捕获剩余参数,如

(#(println "Hello" (apply str (interpose " and " %&))) "Jim" "John" “杰米”)

来自 Clojure 文档

Anonymous function literal (#())
#(...) => (fn [args] (...))
where args are determined by the presence of argument literals taking the 
form %, %n or  %&. % is a synonym for %1, %n designates the nth arg (1-based), 
and %& designates a rest arg. This is not a replacement for fn - idiomatic 
used would be for very short one-off mapping/filter fns and the like. 
#() forms cannot be nested.

#(println "Hello, world!") -> no arguments

#(println (str "Hello, " % "!")) -> 1 argument (% is a synonym for %1)

#(println (str %1 ", " %2 "!")) -> 2 arguments

and so on. Note that you do not have to use all %ns, the number of arguments expected is defined by the highest n. So #(println (str "Hello, " %2)) still expects two arguments.

You can also use %& to capture rest args as in

(#(println "Hello" (apply str (interpose " and " %&))) "Jim" "John" "Jamey").

From the Clojure docs:

Anonymous function literal (#())
#(...) => (fn [args] (...))
where args are determined by the presence of argument literals taking the 
form %, %n or  %&. % is a synonym for %1, %n designates the nth arg (1-based), 
and %& designates a rest arg. This is not a replacement for fn - idiomatic 
used would be for very short one-off mapping/filter fns and the like. 
#() forms cannot be nested.
没有心的人 2024-12-18 11:25:41

它给你一个错误,你将一个参数传递给你的匿名函数,而该函数期望为零。

匿名函数的元数由内部引用的最高参数决定。

例如

(#(identity [2])) -> arity 0, 0 个参数必须传递

(#(identity [%1]) 14) -> arity 1, 1 参数必须传递

(#(identity [%]) 14) -> (%%1 的别名,当且仅当参数数为 1 时),必须传递 1 个参数

(#(identity [%1 %2] ) 14 13)

(#(identity [%2]) 14 13) ->数量为 2,必须传递 2 个参数

(#(identity [%&]) 14) -> arity n,可以传递任意数量的参数

It is giving you the error that you passed one argument to your anonymous function that was expecting zero.

The arity of an anonymous function is determined by the highest argument referenced inside.

e.g.

(#(identity [2])) -> arity 0, 0 arguments must be passed

(#(identity [%1]) 14) -> arity 1, 1 argument must be passed

(#(identity [%]) 14) -> (% is an alias for %1 if and only if the arity is 1), 1 argument must be passed

(#(identity [%1 %2]) 14 13) or

(#(identity [%2]) 14 13) -> arity 2, 2 arguments must be passed

(#(identity [%&]) 14) -> arity n, any number of arguments can be passed

可是我不能没有你 2024-12-18 11:25:41

您需要使用 %1、%2 等引用参数,以使函数需要那么多参数。

You need to refer to the arguments with %1, %2 etc. to cause the function to require that many arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文