store/[org.clojure/clojure "1.8.0"] clj::clojure.core/when-let

Official Documentation

Add notes
Arities
==================================================
   [bindings & body]

Docstring
==================================================
  bindings => binding-form test

  When test is true, evaluates body with binding-form bound to the value of test

Source

(defmacro when-let
  "bindings => binding-form test

  When test is true, evaluates body with binding-form bound to the value of test"
  {:added "1.0"}
  [bindings & body]
  (assert-args
     (vector? bindings) "a vector for its binding"
     (= 2 (count bindings)) "exactly 2 forms in binding vector")
   (let [form (bindings 0) tst (bindings 1)]
    `(let [temp# ~tst]
       (when temp#
         (let [~form temp#]
           ~@body)))))

Example 1

Edit
;; 2 is a truthy value so the body will be evaluated
(when-let [a 2]
  (+ a a))
;; => 4

;; nil is falsey so (+ nil nil) - which would throw a null pointer exception if
;; evaluated is not evaluated. This makes a very convenient guard for
;; conditional imperative blocks which could otherwise error out.

(when-let [a nil]
  (+ a a))
;; => nil

Example 2

Edit
;; Very useful when working with sequences. Capturing the return value 
;; of `seq` brings a performance gain in subsequent `first`/`rest`/`next`
;; calls. Also the block is guarded by `nil` punning.

(defn drop-one
  [coll]
  (when-let [s (seq coll)]
    (rest s)))

(drop-one [1 2 3])
;; => (2 3)

(drop-one [])
;; => nil

Uses on crossclj