store/[org.clojure/clojure "1.8.0"] clj::clojure.core/some->

Community Documentation

Edit

Arities

[expr & forms] -> Val

Docs

Threads expr through forms as if by clj::clojure.core/-> but short circuits if a nil is generated.

Notes

Handy for dealing with nil punning together with Java interop which is not nil safe.

See Also

Source

(defmacro some->
  "When expr is not nil, threads it into the first form (via ->),
  and when that result is not nil, through the next etc"
  {:added "1.5"}
  [expr & forms]
  (let [g (gensym)
        steps (map (fn [step] `(if (nil? ~g) nil (-> ~g ~step)))
                   forms)]
    `(let [~g ~expr
           ~@(interleave (repeat g) (butlast steps))]
       ~(if (empty? steps)
          g
          (last steps)))))

Example 1

Edit
(defn foo [x]
  (some-> x
          (.substring 1)))
;; => #'user/foo

(foo nil)
;; => nil

(foo "bar")
;; => "ar"

Uses on crossclj