Skip to content

Clojure Cheatsheet

Modern dialect of Lisp that runs on the JVM, CLR, and JavaScript engines.

01

Getting Started

Hello World

Clojure is a Lisp — code is data in parentheses.

clojure
(println "Hello, World!")

Defining

def defines vars, defn defines functions.

clojure
(def x 10)
(defn add [a b] (+ a b))
(println (add 3 4))

Collections

Vectors [], maps {}, sets #{}.

clojure
(def lst [1 2 3])
(def m {:a 1 :b 2})
(def s #{1 2 3})

REPL

Clojure is designed for REPL-driven development.

clojure
;; lein repl
;; clj -M:repl
02

Functions & Higher-Order

Anonymous

#() is shorthand for anonymous functions; % is the arg.

clojure
(fn [x] (* x x))
#(* % %)

Map/Filter/Reduce

Core sequence operations.

clojure
(map inc [1 2 3])       ;; (2 3 4)
(filter even? [1 2 3 4])  ;; (2 4)
(reduce + [1 2 3])        ;; 6

Threading

-> threads first, ->> threads last position.

clojure
(-> 5 inc inc)           ;; 7
(->> [1 2 3] (map inc)) ;; (2 3 4)

Partial

partial pre-fills arguments.

clojure
((partial + 10) 5)  ;; 15
((partial * 2) 3 4)  ;; 24
03

Data & State

Destructuring

Destructuring extracts values from collections.

clojure
(let [{:keys [name age]} {:name "Alice" :age 30}]
  (println name age))

Atoms

Atoms provide mutable state with atomic updates.

clojure
(def counter (atom 0))
(swap! counter inc)
(deref counter)  ;; 1
@counter          ;; 1

Loop/Recur

loop/recur for tail-recursive iteration.

clojure
(loop [i 0]
  (if (< i 5)
    (do (println i) (recur (inc i)))
    :done))

Was this helpful?