DEV Community

Cover image for Clojure 101 / operator precedence
icncsx
icncsx

Posted on

Clojure 101 / operator precedence

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)
Enter fullscreen mode Exit fullscreen mode

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)))
Enter fullscreen mode Exit fullscreen mode

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:

Alt Text

Now can you figure out what this does in JavaScript?

3 > 2 && 2 > 1
Enter fullscreen mode Exit fullscreen mode

Apparently, it returns true. I love you JavaScript, but we need a break... truly...

Warmly,
DH

Top comments (0)