Clojure:[_]在函数参数列表中做了什么?

Dav*_*ams 17 syntax arguments vector clojure

我正在研究clojure的乐趣,并想知道_语法在函数参数向量中的作用.

例:

(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)))))
Run Code Online (Sandbox Code Playgroud)

表格中的下划线是做什么的:

#(dotimes [_ exec-count] (func))
Run Code Online (Sandbox Code Playgroud)

har*_*rpo 18

我认为,按照惯例,下划线在Clojure中用作占位符,用于必需但未使用的参数.正如Keith Bennet所说:

在Clojure中,下划线用于表示其标识的参数随后未使用.

您的示例与此"用法"一致,因为dotimes不需要第一个参数(即索引器),但表单需要绑定.

  • 而且haskell可能是从prolog那里得到的.我不知道它从哪里开始. (3认同)

Ank*_*kur 12

没有什么特别的,它只是一个命名你不关心的东西的约定,但它仍然是一个名称,可以像普通名称一样使用.

(defn hello [_] (+ 1 _))
(hello 10)
Run Code Online (Sandbox Code Playgroud)

UPDATE

这样做:

(defn hello [a a]  (+ a a))
Run Code Online (Sandbox Code Playgroud)

不会产生错误,因此你可以使用任意多的_;).

注意:以上不是Scheme或CL的情况......嗯,它在clojure背后的理性是什么?