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!
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))
The other answers all good, but when in doubt, you can always just test it out on the REPL:
user=> (or true (do (println "hello") true))
true
user=> (or false (do (println "hello") true))
hello
true
When in doubt, consult the documentation:
or
macro
Usage:(or) (or x) (or x & next)
Evaluates exprs one at a time, from left to right. If a form returns a logical true value, or returns that value and doesn't evaluate any of the other expressions, otherwise it returns the value of the last expression. (or) returns nil.
(Emphasis mine.)
The documentation for and
shows it behaves in the equivalent way too.
As soon as I finished typing this question, I realized I could just look at the documentation for 'or'.
From the docs: "Evaluates exprs one at a time, from left to right. If a form returns a logical true value, or returns that value and doesn't evaluate any of the other expressions, otherwise it returns the value of the last expression. (or) returns nil."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With