Clojure
02 / 02

Clojure: State, Macros & JVM Interop

Clojure: State, Macros & JVM Interop

State via Atoms: Mutable References to Immutable Values

(def counter (atom 0))

; swap! doesn't mutate 0 in place -- it atomically points counter
; to a NEW immutable value, 1
(swap! counter inc)
@counter  ; => 1

; Two threads reading the SAME immutable vector can never interfere
; with each other -- structurally eliminates a whole class of
; race-condition bugs common in mutable-by-default languages

Data Transformation Pipelines

(->> transactions
     (filter #(> (:amount %) 100))
     (map :customer-id)
     (frequencies))
; Threading macro (->>) expresses filter -> extract -> count concisely

Java Interop

(.toUpperCase "hello")  ; => "HELLO" -- direct Java method call
; Clojure compiles to JVM bytecode -- immediate access to the vast
; existing Java library ecosystem, no separate ecosystem needed

Macros: Code That Writes Code

A macro operates at COMPILE TIME, transforming code itself before it's compiled -- distinct from a regular function, which operates on already-evaluated values at runtime. Homoiconicity (code is literally represented as the same lists Clojure data uses) is what makes this feel like a natural, first-class part of the language rather than a bolted-on templating system.

REPL-Driven Development

Clojure development culture emphasizes keeping a REPL connected directly to a running application -- redefine a single function and see the change take effect immediately, without a full restart. A distinctively fast, interactive feedback loop.

Where Clojure Fits Well

  • Concurrent, highly-parallel backend systems -- immutable data plus atoms/refs avoid shared-mutable-state race conditions.

  • Data-processing and data-transformation-heavy applications -- rich core functions over the seq abstraction express pipelines concisely.

  • Teams already invested in the JVM wanting functional programming's benefits without leaving that ecosystem.

  • Favors composing focused libraries (Ring, routing, DB access) over one large, opinionated framework.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free