Introduction

Basically, destructuring allows you to create local bindings in a very succint (and elegant) syntax.

In this article, we will demonstrate the basics of destructuring in clojure, using KLIPSE.

Destructuring a vector

The simplest example is destructuring the first n values of a vector:

(def point [5 7])

(let [[x y] point]
    {:x x
         :y y})

A more advanced example is splitting a vector into a head and a tail:

(def indexes [1 2 3])

(let [[x & more] indexes]
  {:x x :more more})

It’s also worth noticing that you can bind the entire vector to a local using the :as directive.

(def indexes [1 2 3])

(let [[x & more :as full-list] indexes]
  {:x x :more more :full-list full-list})

Destructuring a map

Simple destructuring on a map is as easy as choosing a local name and providing the key.

(def point {:x 5 :y 7})

(let [{the-x :x the-y :y} point]
         {:x the-x :y the-y})

As the example shows, the values of :x and :y are bound to locals with the names the-x and the-y.

Usually, you want to create locals with the same name as the keys of the map.

In this case, the syntax becomes even simpler, using the :keys directive:

(def point {:x 5 :y 7})

(let [{:keys [x y]} point]
  (+ x y))

As with vectors, you can bind the entire map to a local using the :as directive.

Here is how to combine :keys and :as.

(def point {:x 5 :y 7})

(let [{:keys [x y]} point]
  (+ x y))

And here is how to combine destructuring of vectors and maps (thank you Paul Salaberria):

(let [[[one :as vec-one]
[{:keys [k] :as themap} :as vec-two] :as x]
[[1] [{:k "val"}]]]
{:x x
:one one
:vec-one vec-one
:k k
:themap themap
:vec-two vec-two})

In the next article, we will show more advanced usages og destructuring in clojure.