The Final Frontier: Mixing and Matching

So far we have seen how to destructure sequential things like vectors and lists. We’ve also seen how to destructure our way into maps. What we haven’t done yet is destructure a mixture of the two, perhaps a map in a vector or a vector in a map. Fortunately, it’s just more of the same: to destructure a mix of sequences and maps, you mix the destructuring syntax in exactly the way that you would guess.

Take this vectors-inside-of-a-map conglomeration:

 (​def​ author {:name ​"Jane Austen"
  :books [{:title ​"Sense and Sensibility"​ :published 1811}
  {:title ​"Emma"​ :published 1815}]})

We can get hold of Jane’s name and the information about Emma with a simple

 (​let​ [{name :name [_ book] :books} author] ...

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.