macro | since v0.0-1798 | imported clojure.core/some-> | Edit |
(some-> expr & forms)
When expr
is not nil, threads it into the first form (via ->
), and when that
result is not nil, through the next, etc.
This is a safe navigation operator.
Useful for interop chaining where nulls might appear (seen here):
(some-> pane .getParent .getChildren (.remove pane)))
When expr is not nil, threads it into the first form (via ->), and when that result is not nil, through the next etc
(defmacro some->
{: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)))))