A somewhat ugly solution that should work in Clojure,
(let [expensive-result (or (condition1) (do-expensive-computation)] (cond (condition1) (consequent1) (condition2) (consequent2)))
This requires that condition 1 be checked twice.
Assuming lisp / Clojure in the header means Clojure or (other) lisp, in Common lisp you can do
(let (var) (cond ((condition1) (consequent1)) ((setq var (condition2)) (consequent2))))
but this will not work in Clojure when the local variable is unchanged.
You can use an atom to achieve something similar with Clojure.
(let [v (atom nil)] (cond (condition1) (consequent1) (do (reset! v (expensive)) (condition2 @v)) (consequent2 @v)))
source share