Search code examples
optimizationclojureshort-circuiting

Does Clojure have short-circuit logic?


In many languages, if you write something along the lines of

if (foo() || bar() || foobar()) { /* do stuff */ }

and foo() returns true, then bar() and foobar() will not be evaluated.

Suppose I had the following Clojure code:

(let [a (simple-function args)
      b (complex-function args)
      c (too-lazy-to-optimize-this-function args)]
  (or a b c))

If a evaluates to true, will b and c also be evaluated, or will they be ignored?

Thanks!


Solution

  • Since you answered your own question, note that though in your example b and c may not evaluated in the (or a b c) call, the let binding is evaluated before that so the too-lazy-to-optimize-this-function call is evaluated anyway. Clojure isn't as lazy as that.

    To be clear: to conditionally evaluate the function calls, you need to put the expression evaluating them in the or call, basically:

    (or (simple-function args)
        (complex-function args)
        (too-lazy-to-optimize-this-function args))