Clojure Explained: A Modern Lisp on the JVM

A small online shop records every order in one shared list, and three people touch that list at once: the website adds orders, the packing desk marks them shipped, and the owner totals the day's revenue. In most languages, that shared list is a quiet source of bugs. One function updates a total in place, another reads it halfway through, and the number on the screen depends on who ran first. The shop does not need a bigger framework. It needs data that cannot be changed underneath the reader, so a total computed twice gives the same answer twice. That is the problem Clojure was built for, and it is the reason its parentheses are worth a second look.

What I would check before using it

My judgement is that Clojure is the right call when your system is a data pipeline or a backend that juggles shared state, and the wrong call when the team needs to hire quickly or ship a first version this month. The first check is the platform. If the Java ecosystem is already in place, Clojure costs almost nothing to adopt, because every Java library works and deployment stays exactly where it is. If the shop runs on anything else, the JVM is a heavy guest to invite for one service.

The second check is the team. Prefix notation and parentheses take a week or two to stop feeling strange, and the REPL workflow, where you grow a program by evaluating small pieces against live data, is a genuine change of habit. A team that enjoys that style will move fast. A team that wants familiar syntax and ready answers for every error will move slowly, and Clojure's error messages, which arrive through the JVM stack, do not help beginners.

The third check is the problem itself. Clojure earns its keep when many things happen at once and correctness matters more than raw speed: order totals, inventory counts, event processing. It earns less when the job is a simple CRUD app, where a mainstream framework gets you further with less learning.

The verdict: pick Clojure when immutable data and the JVM together solve a real problem you already have. The recommendation flips when hiring speed or team familiarity dominates, which points to Java or Kotlin, or when there is no JVM in the picture at all.

A worked example that is not a toy

The shop's orders arrive as plain maps, one per order, with the day, the item, the quantity, and the price in rupees. The job is to total revenue per day and print a grand total. No classes, no database, just data and functions:

(def orders
  [{:day "Mon" :item "mug" :qty 2 :price 850}
   {:day "Mon" :item "notebook" :qty 1 :price 1200}
   {:day "Tue" :item "mug" :qty 3 :price 850}
   {:day "Tue" :item "pen" :qty 5 :price 150}
   {:day "Wed" :item "notebook" :qty 2 :price 1200}
   {:day "Wed" :item "gift-card" :qty 1}])

(defn order-total [order]
  (* (:qty order) (or (:price order) 0)))

(def by-day (group-by :day orders))

(doseq [[day day-orders] (sort-by first by-day)]
  (println day (reduce + (map order-total day-orders))))

(println "Grand total:" (reduce + (map order-total orders)))
(println "Original first order unchanged:" (first orders))

Output:

Mon 2900
Tue 3300
Wed 2400
Grand total: 8600
Original first order unchanged: {:day Mon, :item mug, :qty 2, :price 850}

Read it from the inside out. group-by collects the orders under their day, map turns each order into its total, and reduce adds them up. The last line is the point of the whole language: after all that work, the first order is exactly as it started, because nothing in the program mutated it. Any other part of the system can still read that order and trust it.

Now the part that went wrong. The first version of order-total was simpler, and it looked correct:

(defn order-total [order]
  (* (:qty order) (:price order)))

The gift card has no price, because nobody pays for a gift card at the till in the same way, so (:price order) returns nil, and multiplying by nil stops the program with a NullPointerException at order-total, complaining that it cannot invoke a method because x is null. In a language with nulls, a missing field is a crash waiting for the one order that lacks it. The fix is to decide what a missing price means before the maths runs: here it means zero, so (or (:price order) 0) supplies it, and the totals come out right. The lesson is not to fear nil, it is to handle it at the boundary where the data arrives, not three functions later.

Common mistakes and how to fix them

1. Expecting assoc to change the original. Writing (assoc user :age 26) and then reading user still gives {:name "Ayesha" :age 25}, because assoc returns a new map and leaves the old one alone. That is the feature, not a bug, but it surprises anyone used to mutation. The fix is to use the returned value: (def older (assoc user :age 26)).

2. Adding a bare value to a map with conj. Writing (conj {:a 1} :b) fails with IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Keyword. A map is a collection of key-value pairs, so conj needs a pair, not a single keyword. The fix is (conj {:a 1} [:b 2]), or (assoc {:a 1} :b 2).

3. Side effects inside a lazy map. Writing (map println [1 2 3]) at the top of a script prints nothing at all, because map is lazy and nothing ever asks for its results. The fix is doseq when you want effects, (doseq [n [1 2 3]] (println n)), or doall when you want the realised list, (doall (map println [1 2 3])).

Where this leaves you

Clojure is worth learning when shared, changing state is the actual source of your bugs, because immutability removes a whole class of them instead of managing them. It is not worth adopting for a simple app, a team that needs to hire fast, or a codebase with no JVM in sight. The next step is small and concrete: take one function that totals or transforms a list in a language you already know, rewrite it in Clojure at the REPL, and watch what happens when the original data stays untouched. If that feeling of safety is worth the parentheses to you, keep going. If it feels like ceremony, you have your answer.

Further reading

Comments

Popular posts from this blog

Swift Explained: Apple's Language for iPhone, Mac, and More

Rust Explained: Memory Safety Without a Garbage Collector

Zig Explained: A Modern Systems Language Without the Magic