为什么Clojure的地图破坏看起来逆转? [英] Why does Clojure's map destructuring look reversed?

查看:141
本文介绍了为什么Clojure的地图破坏看起来逆转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解构地图与我相反。任何人都可以解释发生了什么?



我期望这是正确的解构地图形式


; =>(let [{:aa:bb} {:a 1:b 2}] [ab])



$ b b

返回异常不支持的绑定形式::clojure.core / destructure / pb - 4541(core.clj:4029)。 Clojure的文档说,下面是正确的方法。









b

; =>(let [{a:ab:b} {:a 1:b 2}] [ab])
[1 2]


解构地图时会发生什么?

解决方案

实际上它是有道理的。它说:将符号'a'绑定到与关键字相关联的值:a



当你的地图使用关键字作为键时,你知道这一点吗?

 (let [{:keys [ab]} {:a 1:b 2}] [ab])

非常整洁优雅!



符号或字符串。


Destructuring a map looks reversed to me. Can anybody explain what is happening?

I expect that this is the right form of destructuring a map

;=> (let [{:a a :b b} {:a 1 :b 2}] [a b])

which returns Exception Unsupported binding form: :a clojure.core/destructure/pb--4541 (core.clj:4029). Clojure documentations say that below is the right way. But it looks that keys and values are reversed.

This should be the right way:

;=> (let [{a :a b :b} {:a 1 :b 2}] [a b]) [1 2]

What is happening when destructuring a map?

解决方案

It is not really reversed, actually it makes sense. It says: bind to symbol 'a' to value that is associated with the keyword :a

Are you aware of this when your map uses keywords as keys?

(let [{:keys [a b]} {:a 1 :b 2}] [a b])

Much neater and elegant!

Other variants exist if your keys are symbols or strings.

这篇关于为什么Clojure的地图破坏看起来逆转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆