clojure-style-guide
clojure-style-guide copied to clipboard
(into {} (for ... )) — curly or square brackets to return map entries
trafficstars
E.g.
(into {}
(for [n [:a :b :c]]
[n (name n)]))
;; vs.
(into {}
(for [n [:a :b :c]]
{n (name n)}))
:smile:
nrepl> (time (do ;; vs.
(into {}
(for [n (range 10000000)]
[n n]))
nil))
"Elapsed time: 14067.538884 msecs"
nil
nrepl> (time (do ;; vs.
(into {}
(for [n (range 10000000)]
{n n}))
nil))
"Elapsed time: 17612.829969 msecs"
nil
For performance, the [k v] vector pair should be preferred although the difference isn't really significant except for large inputs.
Furthermore from a types sense (into {} (e -> [[k v]] -> {[k v]}) makes somewhat sense than the map merge form of into.
Because seq on a map produces a list of vectors, I find it more natural to use vectors for building a map also.
(seq {:a 1 :b 2})
=> ([:a 1] [:b 2])
For your special use case, there is also for-map from Prismatic plumbing.