Destructuring in Clojure

10 Jan 2013

2 What is Destructuring?

One of Speical Froms in clojure. Kind of Syntactic sugar?

Clojure supports abstract structural binding, often called destructuring, in let binding lists, fn parameter lists, and any macro that expands into a let or fn. The basic idea is that a binding-form can be a data structure literal containing symbols that get bound to the respective parts of the init-expr. The binding is abstract in that a vector literal can bind to anything that is sequential, while a map literal can bind to anything that is associative.

2.1 Vector binding.

Anything supports nth . & will bind the remainder of the sequence. :as will bind the whole sequence.

(let [[a b c & d :as e] [1 2 3 4 5 6 7]]
  [a b c d e])
->[1 2 3 (4 5 6 7) [1 2 3 4 5 6 7]]

(let [[[x1 y1][x2 y2]] [[1 2] [3 4]]]
  [x1 y1 x2 y2])
->[1 2 3 4]

(let [[a b & c :as str] "asdjhhfdas"]
  [a b c str])
->[\a \s (\d \j \h \h \f \d \a \s) "asdjhhfdas"]

2.2 Map binding.

Bind to Associative things. map/vector/string/array(only integer keys except the map).

:as works as in vector binding.What's more, :or followed by another map can be used for default values or not-exist keys in the original map(named init-expr).

(let [{a :a, b :b, c :c, :as m :or {a 2 b 3}} {:a 5 :c 6}]
  [a b c m])
->[5 3 6 {:c 6, :a 5}]
;; a is bound to the value of key ':a' in init-expr.
;; c is bound to the default value

:keys for a simpler situation: symbol's name is the same as the key. :strs and :syms for string and symbol keys.

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

Nested form, no need for special attention.

(let [{j :j, k :k, i :i, [r s & t :as v] :ivec, :or {i 12 j 13}}
{:j 15 :k 16 :ivec [22 23 24 25]}]
[i j k r s t v])
-> [12 15 16 22 23 (24 25) [22 23 24 25]]

3 TODO How did this happen?