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

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

问题描述

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天全站免登陆