2
votes

I'm trying to understand how let bindings work in closure in regards to maps. From my understanding let is followed by a vector where the first item is the symbol I want bound, followed by the value I want to bind it to. So

(let [a 1 b 2] a) 

would give a the value of 1.

So if I declare map such as

(def people {:firstName "John" :lastName "Doe"})

And I want to bind the key firstName then this would be the correct form for a simple "Hello Person"

(let [personName (:firstName people)] (str "hello " personName))

This works, however on the Clojure website http://clojure.org/special_forms#binding-forms they show a different form which also works

(let [{personName :firstName} people] (str "hello " personName))

Both code snippets work and I understand why the first version works but I'm confused on the syntax on the second. Is this just syntactical sugar or a duplicate way of working and is one more idiomatic than the other?

2
See the section on Map binding destructuring in the document you linked to. - Barmar
There's no key :name in your map. - Barmar
Sorry I misnamed the key. - Brandon

2 Answers

3
votes

The last form is Map binding destructuring. It's most useful if you want to bind multiple variables to different elements of the same map:

(let [{firstName :firstName lastName :lastName address :address} people]
    (str "hello " firstName " " lastName ", I see you live at " address))

If you were to write this out using the previous syntax, it would be:

(let [firstName (:firstName people) lastName (:lastName people) address (:address people)]
    (str "hello " firstName " " lastName ", I see you live at " address))

As you see, this saves you from having to repeat people in each binding. And if the map is the result of a computation, it avoids having to bind a variable to that so you can repeat it.

Destructuring also allows you to lay out the binding construct in a format that's similar to the data structure you're accessing. This makes it more idiomatic for complex structures.

2
votes

Just to be clear (and not too pedantic, I hope) ...

In

(let [personName (:firstName people)] (str "hello " personName))

you are not binding the key :firstname to anything; you are binding the identifier personName to the application of keyword :firstname as a function to the map people.

In

(let [{personName :firstName} people] (str "hello " personName))

you are destructuring the map people, binding the identifier personName to its value at key :firstname.


PS

This miracle is worked by the destructure function invoked by the let form. It can be helpful to look at what it does. For example,

(destructure '[{personName :firstName} people])
;[map__779
; people
; map__779
; (if
;  (clojure.core/seq? map__779)
;  (clojure.lang.PersistentHashMap/create (clojure.core/seq map__779))
;  map__779)
; personName
; (clojure.core/get map__779 :firstName)]

I picked up this idea here.