Clojure:[_]在函数参数列表中做什么? [英] Clojure: What does [_] do in a Functions Argument List?
问题描述
例如:
我通过clojure的喜悦工作,想知道函数参数向量中的_ (可用处理器
(.availableProcessors(Runtime / getRuntime)))
(prnavailable processors:可用处理器)
(def pool
(Executors / newFixedThreadPool(+ 2 available-processors)))
(defn dothreads!
[func & {thread-count:threads exec-count:times:或{thread-count 1 exec-count 1}}]
(dotimes [t thread-count]
(.submit pool# [_ exec-count](func)))))
:
#(dotimes [_ exec-count](func))
/ pre>
解决方案我相信Clojure中使用下划线作为必需的但未使用的参数的占位符。由于 Keith Bennet 提供:
在Clojure中,下划线用于表示随后使用它标识的
参数。
您的示例与此usage一致,因为
dotimes
(它是一个索引器)的第一个参数是不需要,但绑定是需要的形式。I am working through the joy of clojure and am wondering what the _ syntax does in a functions argument vector.
Example:
(def available-processors (.availableProcessors (Runtime/getRuntime))) (prn "available processors: " available-processors) (def pool (Executors/newFixedThreadPool (+ 2 available-processors))) (defn dothreads! [func & {thread-count :threads exec-count :times :or {thread-count 1 exec-count 1}}] (dotimes [t thread-count] (.submit pool #(dotimes [_ exec-count] (func)))))
What is the underscore doing in the form:
#(dotimes [_ exec-count] (func))
解决方案I believe that underscore is used in Clojure, by convention, as a placeholder for a required but unused argument. As Keith Bennet puts it:
In Clojure, the underscore is used idiomatically to indicate that the argument it identifies is not subsequently used.
Your example is consistent with this "usage," since the first argument to
dotimes
, which is an indexer, is not needed, but the binding is required by the form.这篇关于Clojure:[_]在函数参数列表中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!