liveBook · Manning

Some->

As explained in Clojure: The Essential Reference

some-> is a Clojure macro that threads a value through forms as the second argument of each successive call and stops with nil when an intermediate result is nil.

Use `some->` when a pipeline may produce `nil` before reaching a function that cannot accept it. Like `->`, it passes each result as the second argument of the next form. Unlike `->`, it stops and returns `nil` as soon as a result is `nil`. section 2.3.4

The contract is compact:

  • The initial expression is required; subsequent forms are optional. A bare form is wrapped as a call, and each form must begin with something callable. section 2.3.4
  • If the pipeline completes, its result is the last form’s result. For last-argument threading with the same nil stop, use `some->>`. section 2.3.4

Stop before a call that would fail

`(-> {:a 1 :b 2} :c inc)` throws `NullPointerException`: the absent key yields `nil`, which reaches `inc`. `(some-> {:a 1 :b 2} :c inc)` returns `nil` instead. section 2.3.4

Guard an environment lookup

`(or (some-> (System/getenv "PORT") Integer.) 4444)` converts a present `PORT` value and uses `4444` when the lookup yields `nil`. The fallback comes from `or`, not from `some->`. section 2.3.4

`some->` is not a configurable defaulting function: its nil result is fixed. If a nil argument should be replaced before a function runs, `fnil` provides that different behavior. section 2.2.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, argument placement, return behavior, and nil short-circuiting through examples, without showing macro expansion.
Examples
Contrasts a missing map key with ordinary threading and guards an environment-variable conversion with a fallback.
What it assumes
Assumes familiarity with Clojure expressions and calls while spelling out how forms are threaded.
What you can do after
Apply it via Clojure's some-> macro to stop nil-producing pipelines before later calls run.
How it connects
Relates some-> to ->, some->>, Java interop, and fnil.

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.