Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure let binding forms

Tags:

clojure

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?

like image 309
Brandon Avatar asked Jul 17 '26 14:07

Brandon


1 Answers

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.

like image 156
Barmar Avatar answered Jul 20 '26 11:07

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!