问题描述
从此问题继续:
使用clojure的地图访问可以通过多种方式完成。
def m {:a 1}
(get m:a);; => 1
(:am);; => 1
;; => 1
我知道我主要使用第二种形式,有时第三种,很少
从此问题继续:
使用clojure的地图访问可以通过多种方式完成。
def m {:a 1}
(get m:a);; => 1
(:am);; => 1
;; => 1
我知道我主要使用第二种形式,有时第三种,很少
从我们看到
有时候在Clojure的引擎盖下看看有什么好处。如果您查看地图中调用
的样式,您会看到:
):
。
对于使用key和not-found-value调用的映射,也是如此。那么,有什么优势呢?由于两种方法都归结到几乎相同,性能明智我不会说什么。它只是语法方便。
Following up from this question: Idiomatic clojure map lookup by keyword
Map access using clojure can be done in many ways.
(def m {:a 1}
(get m :a) ;; => 1
(:a m) ;; => 1
(m :a) ;; => 1
I know I use mainly the second form, and sometimes the third, rarely the first. what are the advantages (speed/composability) of using each?
From the clojure web page we see that
Sometimes it is rewarding to take a look under the hoods of Clojure. If you look up what invoke
looks like in a map, you see this:
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/APersistentMap.java#L196
It apparently calls the valAt
method of a map.
If you look at what the get
function does when called with a map, this is a call to clojure.lang.RT.get
, and this really boils down to the same call to valAt
for a map (maps implement ILookUp because they are Associatives):
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/RT.java#L634.
The same is true for a map called with a key and a not-found-value. So, what is the advantage? Since both ways boil down to pretty much the same, performance wise I would say nothing. It's just syntactic convenience.
这篇关于什么优势是使用'get'而不是访问地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!