Skip to content

clojure.core/when-let

View this page on ClojureDocs

Type: macro Added: Clojure 1.0 Examples: 8 Runnable: 6
([bindings & body])

bindings => binding-form test

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

Examples

by kotarak on
by jafingerhut on
by mattvvhat on
by ertugrulcetin on
by bfabry on . May have evaluation errors.
by arlicle on
by Jjunior130 on . May have evaluation errors.
by arcanjoaq on

Note by gstamp

The difference between when-let and if-let is that when-let doesn't have an else clause and and also accepts multiple forms so you don't need to use a (do...).

Note by n2o

The word "bindings" seems not to be correct here. In fact when-let only accepts one binding and not multiple ones. So "bindings" might be confusing, at least it was for me.

Note by Dave Y.

Agreed. It ought to be "binding" for clarity.

Note by HomoEfficio

(when-let [name test]
  (do-something-with-name))

In the example above, test does not have to be a predicate. test can be any value which is like (seq coll) or 3, [1 2], or so.

If test is neither false nor nil, test is bound to name.

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.