Collections

'(1 2 3) / (list 1 2 3) Linked list; efficient add/remove at the front; used for code

Indexed; efficient add at the end; the default sequential collection

Key-value (hash map; array map when small) Immutability: assoc, conj, and friends return new collections. The originals are unchanged, and structural sharing keeps this efficient.

Equality: collections compare by value. (= [1 2] '(1 2)) is true (both are sequential).

;; Vectors
(def v [10 20 30])
(get v 1)                 ; 20
(v 1)                     ; 20: vectors are functions of their indices
(nth v 2)                 ; 30
(conj v 40)               ; [10 20 30 40]
(assoc v 0 99)            ; [99 20 30]
(pop v)                   ; [10 20]
(peek v)                  ; 30
(subvec v 1)              ; [20 30]
(count v)                 ; 3
(vec '(1 2 3))            ; [1 2 3]
(mapv inc v)              ; [11 21 31]

;; Lists
(def l '(1 2 3))
(conj l 0)                ; (0 1 2 3): conj adds where it's efficient
(first l) (rest l) (peek l) (pop l)   ; 1 (2 3) 1 (2 3)

;; Maps
(def m {:name "Ana" :age 30})
(get m :name)             ; "Ana"
(:age m)                  ; 30
(m :age)                  ; 30: maps are functions of their keys
(get m :email "none")     ; "none" (default)
(assoc m :email "a@x.com")
(dissoc m :age)
(update m :age inc)       ; {:name "Ana", :age 31}
(merge m {:city "Pune"})
(keys m) (vals m)
(contains? m :name)       ; true
(select-keys m [:name])   ; {:name "Ana"}
(zipmap [:a :b] [1 2])    ; {:a 1, :b 2}
(sorted-map :b 2 :a 1)    ; {:a 1, :b 2}
(update-vals {:a 1 :b 2} inc)   ; {:a 2, :b 3} (1.11+)
(update-keys {:a 1} name)       
…