Search code examples
syntaxvectorclojurearguments

Clojure: What does [_] do in a Functions Argument List?


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))

Solution

  • 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.