Destructuring

Destructuring is a form of pattern matching that is common in Clojure. Destructuring allow you to pull out the specific elelments from a collection.

Destructuring is commonly used with the let method for creating local bindings (locally scoped names).

  1. (let [[a b c & d :as e] [1 2 3 4 5 6 7]]
  2. [a b c d e])
  3. (let [[[x1 y1][x2 y2]] [[1 2] [3 4]]]
  4. [x1 y1 x2 y2])
  5. ;; with strings
  6. (let [[a b & c :as str] "asdjhhfdas"]
  7. [a b c str])
  8. ;; with maps
  9. (let [{a :a, b :b, c :c, :as m :or {a 2 b 3}} {:a 5 :c 6}]
  10. [a b c m])

It is often the case that you will want to bind same-named symbols to the map keys. The :keys directive allows you to avoid the redundancy:

  1. (let [{fred :fred ethel :ethel lucy :lucy} m] )

This can be written in a shorter form as follows:

  1. (let [{:keys [fred ethel lucy]} m] )

As of Clojure 1.6, you can also use prefixed map keys in the map destructuring form:

  1. (let [m {:x/a 1, :y/b 2}
  2. {:keys [x/a y/b]} m]
  3. (+ a b))

As shown above, in the case of using prefixed keys, the bound symbol name will be the same as the right-hand side of the prefixed key. You can also use auto-resolved keyword forms in the :keys directive:

  1. (let [m {::x 42}
  2. {:keys [::x]} m]
  3. x)