Sean Devlin wrote:
> Hey everyone,
> I was working with an object that implements java.util.Map today, and
> I had to turn it into a Clojure map.
>
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html
>
> I cam up with this utility fn
>
> (defn read-map
> "Designed to turn a java.util.Map into a Clojure map."
> [a-map]
> (into {}
> (map
> #(hash-map (.getKey %) (.getValue %))
> (seq a-map))))
Why the (map) and seq? Does (into {} a-map) not work for some
implementations java.util.Map?
(into {}
(doto (java.util.HashMap.)
(.put :a 1)
(.put :b 2)))
;; => {:a 1, :b 2}
> What if hash-map automatically cast a java.util.Map to a Clojure map?
That'd really be changing the meaning of (hash-map) -- it doesn't even
do that with Clojure maps, you can't do (hash-map {:a 1}).
Better to stick with (into {}) -- it's actually both one character
shorter and more generally applicable.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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