Say I have a map {:x 1}
.
I noticed that I can lookup :x
either by applying it to the map:
(:x {:x 1})
;=> 1
或通过将地图应用于关键字:
({:x 1} :x)
;=> 1
两种形式有什么区别?
Say I have a map {:x 1}
.
I noticed that I can lookup :x
either by applying it to the map:
(:x {:x 1})
;=> 1
或通过将地图应用于关键字:
({:x 1} :x)
;=> 1
两种形式有什么区别?
Both maps and keywords implement
IFn
and therefor can be used as functions. The function they implement isget
. So it basically always translates to(get map key default)
.因此,有一些自己可以弥补的经验法则;要考虑的一些事情:
({"a" 1} "a")
)(nil {:a 1})
fails, but({:a 1} nil)
works); if you are unsure about both: useget