在Clojure中创建地图条目 [英] Create a map entry in Clojure
本文介绍了在Clojure中创建地图条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建单个地图条目的内置 Clojure方法是什么?
What is the built-in Clojure way (if any), to create a single map entry?
换句话说,我想要(地图输入键值)
之类的东西。换句话说,结果应大致等于(第一个{键值})
。
In other words, I would like something like (map-entry key value)
. In other words, the result should be more or less equivalent to (first {key value})
.
备注:
- 当然,我已经尝试了谷歌搜索,只发现了地图条目?但是,该文档没有链接的资源。
- 我知道
(第一个{1 2})
返回[1 2]
,这似乎是一个向量。但是:
- Of course, I already tried googling, and only found map-entry? However, this document has no linked resources.
- I know that
(first {1 2})
returns[1 2]
, which seems a vector. However:
(class (first {1 2}))
; --> clojure.lang.MapEntry
(class [1 2])
; --> clojure.lang.PersistentVector
- 我检查了源代码,然后知道 MapEntry 和 PersistentVector 扩展 APersistentVector (所以
MapEntry
还是一个向量)。但是,问题仍然是,我是否可以根据Clojure代码创建MapEntry
实例。 - 最后,但并非最不重要的是:否,在Clojure中没有内置的方法可以做到这一点(这也是一个有效的答案(我非常怀疑是这种情况,只是想确保我没有偶然错过任何事情)。
- I checked in the source code, and I'm aware that both MapEntry and PersistentVector extend APersistentVector (so
MapEntry
is more-or-less also a vector). However, the question is still, whether I can create aMapEntry
instance from Clojure code. - Last, but not least: "no, there is no built in way to do that in Clojure" is also a valid answer (which I strongly suspect is the case, just want to make sure that I did not accidentally miss something).
推荐答案
不,在Clojure中没有内置的方法可以做到这一点也是有效的答案
"no, there is no built in way to do that in Clojure" is also a valid answer
是的,很不幸,这就是答案。我想说的是,您可以做的最好的事情就是自己定义 map-entry
函数:
Yeah, unfortunately that's the answer. I'd say the best you can do is define a map-entry
function yourself:
(defn map-entry [k v]
(clojure.lang.MapEntry/create k v))
这篇关于在Clojure中创建地图条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文