Clojure let binding forms

佐手、 提交于 2019-12-23 23:25:48

问题


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?


回答1:


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:


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.



来源:https://stackoverflow.com/questions/23441019/clojure-let-binding-forms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!