On Mon, 23 Mar 2009 at 03:29, Mark Engelberg wrote:
> But it traverses m twice, which is likely to be less efficient.
I wondered about this too, and actually no. Zipmap is efficient. It constructs
its return map in a single loop from two lazy seqs. Performance is practically
identical to the "for" version:
(defn mapmap1 [f m] (into {} (for [[k v] m] [k (f v)])))
(defn mapmap2 [f m] (zipmap (keys m) (map f (vals m))))
(def m (apply hash-map (range 2000000)))
(time (def m2 (mapmap1 identity m)))
"Elapsed time: 3221.625654 msecs"
(time (def m2 (mapmap2 identity m)))
"Elapsed time: 3322.947888 msecs"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---