Quick reference for Clojure collection literals and the operations you’ll use most on lists, vectors, maps, and sets.
Clojure programs and Clojure data use the same persistent collections. Once you recognize the literal shapes, you can “read the data” and understand most code by inspection.
1(a b c) ; list (usually code)
2'(a b c) ; list as data (quoted)
3[a b c] ; vector (ordered data)
4{:a 1 :b 2} ; map (associative data)
5#{:a :b} ; set (membership / uniqueness)
Lists are optimized for adding/removing at the front. Vectors are optimized for indexed access.
1(conj [1 2] 3) ; => [1 2 3]
2(conj '(1 2) 3) ; => (3 1 2)
If you’re coming from Java, you’ll typically use vectors and maps for most domain data. You’ll see lists most often in code and in macro examples.
Represent a user/order/config as a map, typically with keyword keys:
1(def user {:user/id 42
2 :user/name "Ada"})
3
4(:user/id user) ; => 42
5(get user :user/name) ; => "Ada"
6(assoc user :user/active? true)
7(update user :user/id inc)
Nested access and updates stay readable with get-in, assoc-in, and update-in:
1(get-in {:a {:b 1}} [:a :b]) ; => 1
2(assoc-in {} [:a :b] 1) ; => {:a {:b 1}}
3(update-in {:a {:b 1}} [:a :b] + 10) ; => {:a {:b 11}}
1(def roles #{:admin :billing})
2
3(contains? roles :admin) ; => true
4(roles :admin) ; => :admin ; set-as-function membership
5(roles :guest) ; => nil