liveBook · Manning

When-some

As explained in Clojure: The Essential Reference

when-some is a Clojure macro that lexically binds a single value and evaluates a body of forms only when that value is non-nil.

when-some lexically binds one value and evaluates multiple body forms only when that value is not nil. It returns nil otherwise. section 3.1.2

Its contract is:

  • Accept one binding pair, whose expression may produce a destructured value. section 3.1.2
  • Bind the result in lexical scope and treat the body as an implicit do block. section 3.1.2
  • Run the body for any non-nil value, including false; return nil for nil. section 3.1.2

Non-nil, not merely truthy

(when-some [n "then"] n) ;; "then" (when-some [n nil] n) ;; nil (if-some [n false] n "else") ;; false

How Clojure: The Essential Reference covers it

How deep it goes
Defines the binding contract, implicit do behavior, return values, and false-versus-nil edge case.
Examples
Demonstrates string, nil, and false values with concise conditional-binding expressions.
What it assumes
Assumes familiarity with let-style bindings and evaluable forms while specifying its single-pair contract.
What you can do after
Apply it via Clojure conditional-binding syntax when nil should suppress a body without suppressing false.
How it connects
Contrasts with let, when-let, if-let, and if-some through binding and truth-testing behavior.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.