DEV Community

Cover image for Clojure 101 / operator precedence
icncsx
icncsx

Posted on

2 2

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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay