Skip to content

Instantly share code, notes, and snippets.

@joey-coleman
Forked from john2x/00_destructuring.md
Created November 23, 2015 20:51
Show Gist options
  • Save joey-coleman/afb9f2a8517796b7f3f5 to your computer and use it in GitHub Desktop.
Save joey-coleman/afb9f2a8517796b7f3f5 to your computer and use it in GitHub Desktop.
Clojure Destructuring Tutorial and Cheat Sheet

Vectors

Syntax: [symbol another-symbol] ["value" "another-value"]

(def my-vector [:a :b :c :d])
(def my-nested-vector [:a :b :c :d [:x :y :z]])

(let [[a b c d] my-vector]
  (println a b c d))
;; => :a :b :c :d

(let [[a _ _ d [x y z]] my-nested-vector]
  (println a d x y z))
;; => :a :d :x :y :z

When a destructuring form "exceeds" a vector (i.e. there not enough items in the vector to bind to), nil will be binded to the excess symbols.

(let [[a b c d e f g] my-vector]
  (println a b c d e f g))
;; => :a :b :c :d nil nil nil

You can use :as some-symbol as the last two items in the destructuring form to bind the whole vector to some-symbol

(let [[:as all] my-vector]
  (println all))
;; => [:a :b :c :d]

(let [[a _ _ _ :as all] my-vector]
  (println a all))
;; => :a [:a :b :c :d]

(let [[a _ _ _ [x y z :as nested] :as all] my-nested-vector]
  (println a all x y z nested))
;; => :a [:a :b :c :d [:x :y :z]] :x :y :z [:x :y :z]

Function args usage:

(defn foo [a b & [x y z]]
  (println a b x y z))
(foo :a :b) ;; => :a :b nil nil nil
(foo :a :b :x) ;; => :a :b :x nil nil
(foo :a :b :x :y :z) ;; => :a :b :x :y :z

Hashmaps

Syntax: {symbol :key, another-symbol :another-key} {:key "value" :another-key "another-value"}

(def my-hashmap {:a "A" :b "B" :c "C" :d "D"}) (def my-nested-hashmap {:a "A" :b "B" :c "C" :d "D" :q {:x "X" :y "Y" :z "Z"}})

(let [{a :a d :d} my-hashmap] (println a d)) ;; => A D

(let [{a :a b :b {x :x y :y} :q} my-nested-hashmap] (println a b x y)) ;; => A B X Y

(let [{a :a none :none b :b :as all} my-hashmap] (println a none b all)) ;; => A nil B {:a "A" :b "B" :c "C" :d "D"}

Default values for when a key is not in the map

Shortcuts

Vectors + Hashmaps + Vectors...


Compojure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment