Surprise! I tricked you (😆). There is no operator precedence in Clojure because there are no operators - only functions and arguments.
All functions evaluate left to right and inside out. Function first, then args.
(/ 1 2)
(/ 500 20)
(= "foo" "bar")
(not true)
Ok, that seems trivial. How about something more complicated?
(defn leap-year? [year]
(or
(and
(= (rem year 4) 0)
(> (rem year 100) 0))
(= (rem year 400) 0)))
I'm telling you this is such an underrated feature of using Clojure. I can focus on memorizing the important stuff - not horrendously lengthy precedence tables such as this:
Now can you figure out what this does in JavaScript?
3 > 2 && 2 > 1
Apparently, it returns true
. I love you JavaScript, but we need a break... truly...
Warmly,
DH
Top comments (0)