Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created October 14, 2011 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalloy/1288628 to your computer and use it in GitHub Desktop.
Save amalloy/1288628 to your computer and use it in GitHub Desktop.
(defn tricky-logic [x]
(cond (or (cheap-test1)
(expensive-test))
(...body1...)
(and (cheap-test2)
(expensive-test))
(...body2...)
:else (expensive-test)))
;; rewrite to avoid repeating tests *or* code:
(defn tricky-logic [x]
(let [expensive-results (delay (expensive-test))]
(cond (or (cheap-test1)
(force expensive-results))
(...body1...)
(and (cheap-test2)
(force expensive-results))
(...body2...)
:else (force expensive-results))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment