Clojure:[_]在函数参数列表中做什么? [英] Clojure: What does [_] do in a Functions Argument List?

查看:208
本文介绍了Clojure:[_]在函数参数列表中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如:

我通过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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆