Diving into Nested Maps

In the same way that you can use destructuring to dig into several layers of sequential data structures such as lists or vectors, you can also excavate several layers of maps. For example, given this two-level description of Jane Austen:

 (​def​ austen {:name ​"Jane Austen"
  :parents {:father ​"George"​ :mother ​"Cassandra"​}
  :dates {:born 1775 :died 1817}})

we can extract the names of her parents with this:

 (​let​ [{{dad :father mom :mother} :parents} austen]
  (println ​"Jane Austen's dad's name was"​ dad)
  (println ​"Jane Austen's mom's name was"​ mom))

A good way to look at this kind of two-level map destructuring is from the outside in. At the very outside, we have the basic let structure:

 (​let​ [<<something-to-bind-to>> ...

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.