liveBook · Manning

If-some

As explained in Clojure: The Essential Reference

if-some is a Clojure macro that lexically binds a single value and evaluates one of two forms according to whether that value is non-nil.

if-some is a Clojure macro for conditionally evaluating forms after lexically binding one expression. It chooses the then-form when the expression is not nil, including when its value is false; otherwise it evaluates the optional else-form or returns nil. section 3.1.2

Nil differs from false

(if-some [n false] n "else") ;; false (if-some [n nil] n "else") ;; "else" The binding vector contains one symbol-expression pair, and the bound name is available in the selected branch.

How Clojure: The Essential Reference covers it

How deep it goes
Defines the contract, return behavior, binding shape, and nil-versus-false edge case, without expanding macro internals.
Examples
Demonstrates false and nil branches, resource processing, and channel consumption in core.async loops.
What it assumes
Assumes familiarity with Clojure forms and lexical bindings, while specifying the single binding pair and evaluable expression contract.
What you can do after
Apply it to branch on non-nil results while retaining the result in a local binding.
How it connects
Contrasts with if-let and when-some through lexical binding, conditional selection, and different truth tests.

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.