convention | since v0.0-927 | in clojure | Edit |
When unused values require a name, it is common to use _
.
(fn [_ _ a]))
- ignore first two function arguments(let [[_ a _ b] ...])
- only bind names to 2nd and 4th values of a sequenceThe compiler does not specially treat _
. It is a valid symbol, thus it can
serve as a name for a value. It is just convention to reserve this name for
values that are not to be referenced.
Multiple _
's can be used since duplicate names shadow those that come before.
Thus, _
actually holds the value of its last binding.
Create a function whose first two arguments are ignored:
(fn [_ _ a]
(println a))
Ignore the first and third value of a destructured sequence:
(let [ [_ a b _ c]
[1 2 3 4 5] ]
(println a b c))
;; 2 3 5
Ignore return values of debug statements in a let
block:
(let [a 1
_ (println a)
b (+ a 2)
_ (println b)
c (+ b 3)]
...)
It is common to use _
to ignore all but the latest value of a changing atom
inside an add-watch
callback:
(def a (atom 1))
(add-watch a :foo
(fn [_ _ _ s]
(println s)))
(reset! a 2)
;; 2