Pry Open Your Data

To see how we can use destructuring to peel back the layers of our data structures, we’ll start by disassembling something simple and work from there. Take this not-very-intimidating vector:

 (​def​ artists [:monet :austen])

and imagine you wanted to separate out the two keywords. There are a lot of ways to do that separation, but given that we’re only dealing with a two-element vector, we might go for the first function and its convenient sibling, second. So running this:

 (​let​ [painter (first artists)
  novelist (second artists)]
  (println ​"The painter is:"​ painter
 "and the novelist is"​ novelist))

will give you the right values in painter and novelist.

Destructuring provides an alternative ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.