Idiomatic clojure map lookup by keyword -
say have clojure map uses keywords keys:
(def my-car {:color "candy-apple red" :horsepower 450})
i know can value associated keyword either using keyword or map function , other argument:
(my-car :color) ; => "candy-apple red" (:color my-car) ; => "candy-apple red"
i realize both forms can come in handy situations, 1 of them considered more idiomatic straightforward usage shown above?
(:color my-car)
standard. there few reasons this, , won't go of them. here's example.
because :color
constant, , my-car
not, hotspot can inline dynamic dispatch of color.invoke(m)
, can't m.invoke(color)
(in java pseudo-code).
that gets better if my-car
happens record color
field instead of plain map: clojure compiler can emit code check "hey, if my-car
instance of cartype, return my-car.color
; otherwise complicated, slow, hashmap lookup."
Comments
Post a Comment