Re: Proposal: Extend behavior of hash-map

2009-11-18 Thread David Brown
On Wed, Nov 18, 2009 at 06:06:52AM -0800, Sean Devlin wrote: >Do you mean the bean fn? > >http://clojure.org/api#toc120 This does appear to be lazy, although only partially. Things like (keys mybean) invokes the beans, but I think that's more just a consequence of how iterators work in Java/Cloju

Re: Proposal: Extend behavior of hash-map

2009-11-18 Thread Sean Devlin
Do you mean the bean fn? http://clojure.org/api#toc120 On Nov 18, 4:23 am, David Brown wrote: > On Tue, Nov 17, 2009 at 03:24:46PM -0800, Richard Newman wrote: > >Baby, bathwater. Making a persistent map out of a Java map is > >expensive. Not everything that implements Map is concrete; e.g., > >

Re: Proposal: Extend behavior of hash-map

2009-11-18 Thread David Brown
On Tue, Nov 17, 2009 at 03:24:46PM -0800, Richard Newman wrote: >Baby, bathwater. Making a persistent map out of a Java map is >expensive. Not everything that implements Map is concrete; e.g., >spending several seconds making a local persistent Clojure map out of >a distributed hash table proxy, j

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread John Harrop
On Tue, Nov 17, 2009 at 6:24 PM, Richard Newman wrote: > > I wonder if perhaps (into {} a-java-map) should work but no other > > substitutions of a potentially-mutable map for a Clojure map. > > Baby, bathwater. Making a persistent map out of a Java map is > expensive. Not everything that impleme

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Richard Newman
> I wonder if perhaps (into {} a-java-map) should work but no other > substitutions of a potentially-mutable map for a Clojure map. Baby, bathwater. Making a persistent map out of a Java map is expensive. Not everything that implements Map is concrete; e.g., spending several seconds making a

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread John Harrop
On Tue, Nov 17, 2009 at 5:06 PM, Sean Devlin wrote: > Heh. Learn something new every day. > > This also works > > (into {} (System/getProperties)) And I'd much prefer it. Passing a mutable Java map around to functions that expect a map but assume it will never change out from under them creates

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Richard Newman
> The only catch is putAll return void (WHY!) Apparently Java people like having more LOC :) > (defn put-all > [java-map clj-map] > (doto java-map >(.putAll clj-map))) Note that this isn't limited to Clojure maps, so 'clj-map' is probably a bad variable name. It works just as well for co

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Sean Devlin
Very awesome. So this means you can (almost) use the putAll method. The only catch is putAll return void (WHY!) (defn put-all [java-map clj-map] (doto java-map (.putAll clj-map))) Sean On Nov 17, 5:29 pm, Alex Osborne wrote: > Sean Devlin wrote: > > Okay golfers.  Is there a better way

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Alex Osborne
Alex Osborne wrote: > Sean Devlin wrote: >> Okay golfers. Is there a better way to do this? >> >> (defn put-all! >> [java-map clj-map] >> (do >> (doseq [entry clj-map] >> (.put java-map (key entry) (val entry))) >> java-map)) >> >> user=>(put-all (java.util.HashMap. ) {:a 1 :b

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Mark Triggs
How about?: (java.util.HashMap. {:a 1 :b 2 :c 3}) => # Mark Sean Devlin writes: > Okay golfers. Is there a better way to do this? > > (defn put-all! > [java-map clj-map] > (do > (doseq [entry clj-map] > (.put java-map (key entry) (val entry))) > java-map)) > > user

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Alex Osborne
Sean Devlin wrote: > Okay golfers. Is there a better way to do this? > > (defn put-all! > [java-map clj-map] > (do > (doseq [entry clj-map] > (.put java-map (key entry) (val entry))) > java-map)) > > user=>(put-all (java.util.HashMap. ) {:a 1 :b 2 :c 3}) > # > > I already

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Sean Devlin
Okay golfers. Is there a better way to do this? (defn put-all! [java-map clj-map] (do (doseq [entry clj-map] (.put java-map (key entry) (val entry))) java-map)) user=>(put-all (java.util.HashMap. ) {:a 1 :b 2 :c 3}) # I already tried into :) On Nov 17, 5:14 pm, Sean De

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Sean Devlin
Wow. So this works user=>(get (System/getProperties) "os.arch") "x86_64" And this does not user=>((System/getProperties) "os.arch") java.lang.ClassCastException: java.util.Properties cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0) but this does user=>((into {} (System/getProperties)) "o

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Alex Osborne
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.

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Richard Newman
> Heh. Learn something new every day. Heh. Incidentally, because of this property there are only three situations in which you need conversion at all: * To get some additional interface that Clojure's maps provides (e.g., to use them as functions) * To get persistence * To have them print r

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Kevin Downey
user=> (import 'java.util.HashMap) java.util.HashMap user=> (def m (doto (HashMap.) (.put 'a :a) (.put 'b :b))) #'user/m user=> m # user=> (into {} m) {b :b, a :a} user=> (class *1) clojure.lang.PersistentArrayMap user=> On Tue, Nov 17, 2009 at 1:56 PM, Richard Newman wrote: > Sean, > > If the c

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Sean Devlin
Heh. Learn something new every day. This also works (into {} (System/getProperties)) On Nov 17, 4:56 pm, Richard Newman wrote: > Sean, > > If the class implements Map, then it already behaves as an associative   > data structure in Clojure. E.g., > > (map (fn [[k v]] (println v)) >       (doto

Re: Proposal: Extend behavior of hash-map

2009-11-17 Thread Richard Newman
Sean, If the class implements Map, then it already behaves as an associative data structure in Clojure. E.g., (map (fn [[k v]] (println v)) (doto (java.util.HashMap.) (.put "foo" "bar") (.put "baz" "noo"))) (get (doto (java.util.HashMap.) (.put "foo" "bar")

Proposal: Extend behavior of hash-map

2009-11-17 Thread Sean Devlin
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] (i