Re: Recursively convert Java Map to Clojure Map

2020-01-31 Thread Jason Ross
Thanks for the reply. I do need to convert to java hashmaps and arraylists 
because I'm trying to duplicate the testing of a clojure workflow being run 
on a server thats pushing pure java context through it. So in my tests I 
define a clojure map but want to javafy it to force errors to happen when I 
try to evaluate the map as a function. 

It's not clear to my how the postwalk-replace will work for converting 
specific types (clojure maps and lists) to other types (java hashmaps and 
array lists) recursively. Is the extend-protocol method discussed below not 
the way to go?

On Friday, January 31, 2020 at 3:15:29 PM UTC-6, Alex Miller wrote:
>
> You don't need that - Clojure maps *are* Java maps (they implement 
> java.util.Map) and you can pass them into most Java APIs as is (with the 
> caveat that they are made for reading, not for writing).
>
> If you did really want to convert them to hash-maps or whatever, it's 
> pretty easy to do so with a clojure.walk/postwalk-replace.
>
> On Friday, January 31, 2020 at 3:07:24 PM UTC-6, Jason Ross wrote:
>>
>> Hey I know this is super old post but what would the reverse look like, 
>> eg. recursively convert Clojure to java map
>>
>> On Saturday, October 15, 2011 at 4:10:32 AM UTC-5, Baishampayan Ghose 
>> wrote:
>>>
>>> > I have a Java Map contains Map of Maps/List (JSON like map) and have
>>> > to convert to Clojure map (This happens at Java - Clojure Interop), So
>>> > I have written a converter function 'as-clj-map' by modifying the
>>> > clojure walk functions, It works fine, but consume lot of cpu when the
>>> > data structure is quit big. Any suggestions to improve this code?
>>>
>>> What about using protocols for this job?
>>>
>>> (defprotocol ConvertibleToClojure
>>>   (->clj [o]))
>>>
>>> (extend-protocol ConvertibleToClojure
>>>   java.util.Map
>>>   (->clj [o] (let [entries (.entrySet o)]
>>>(reduce (fn [m [^String k v]]
>>>  (assoc m (keyword k) (->clj v)))
>>>{} entries)))
>>>
>>>   java.util.List
>>>   (->clj [o] (vec (map ->clj o)))
>>>
>>>   java.lang.Object
>>>   (->clj [o] o)
>>>
>>>   nil
>>>   (->clj [_] nil))
>>>
>>> (defn as-clj-map
>>>   [m]
>>>   (->clj m))
>>>
>>>
>>> Let me know if this works for you & meets your performance requirements.
>>>
>>> Regards,
>>> BG
>>>
>>> -- 
>>> Baishampayan Ghose
>>> b.ghose at gmail.com
>>>
>>>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/a4762b64-5131-4b8f-b794-da3ce41a0c86%40googlegroups.com.


Re: Recursively convert Java Map to Clojure Map

2020-01-31 Thread Alex Miller
You don't need that - Clojure maps *are* Java maps (they implement 
java.util.Map) and you can pass them into most Java APIs as is (with the 
caveat that they are made for reading, not for writing).

If you did really want to convert them to hash-maps or whatever, it's 
pretty easy to do so with a clojure.walk/postwalk-replace.

On Friday, January 31, 2020 at 3:07:24 PM UTC-6, Jason Ross wrote:
>
> Hey I know this is super old post but what would the reverse look like, 
> eg. recursively convert Clojure to java map
>
> On Saturday, October 15, 2011 at 4:10:32 AM UTC-5, Baishampayan Ghose 
> wrote:
>>
>> > I have a Java Map contains Map of Maps/List (JSON like map) and have
>> > to convert to Clojure map (This happens at Java - Clojure Interop), So
>> > I have written a converter function 'as-clj-map' by modifying the
>> > clojure walk functions, It works fine, but consume lot of cpu when the
>> > data structure is quit big. Any suggestions to improve this code?
>>
>> What about using protocols for this job?
>>
>> (defprotocol ConvertibleToClojure
>>   (->clj [o]))
>>
>> (extend-protocol ConvertibleToClojure
>>   java.util.Map
>>   (->clj [o] (let [entries (.entrySet o)]
>>(reduce (fn [m [^String k v]]
>>  (assoc m (keyword k) (->clj v)))
>>{} entries)))
>>
>>   java.util.List
>>   (->clj [o] (vec (map ->clj o)))
>>
>>   java.lang.Object
>>   (->clj [o] o)
>>
>>   nil
>>   (->clj [_] nil))
>>
>> (defn as-clj-map
>>   [m]
>>   (->clj m))
>>
>>
>> Let me know if this works for you & meets your performance requirements.
>>
>> Regards,
>> BG
>>
>> -- 
>> Baishampayan Ghose
>> b.ghose at gmail.com
>>
>>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/cfe49f86-1995-4098-ae89-208a46d2fc76%40googlegroups.com.


Re: Recursively convert Java Map to Clojure Map

2020-01-31 Thread Jason Ross
Hey I know this is super old post but what would the reverse look like, eg. 
recursively convert Clojure to java map

On Saturday, October 15, 2011 at 4:10:32 AM UTC-5, Baishampayan Ghose wrote:
>
> > I have a Java Map contains Map of Maps/List (JSON like map) and have
> > to convert to Clojure map (This happens at Java - Clojure Interop), So
> > I have written a converter function 'as-clj-map' by modifying the
> > clojure walk functions, It works fine, but consume lot of cpu when the
> > data structure is quit big. Any suggestions to improve this code?
>
> What about using protocols for this job?
>
> (defprotocol ConvertibleToClojure
>   (->clj [o]))
>
> (extend-protocol ConvertibleToClojure
>   java.util.Map
>   (->clj [o] (let [entries (.entrySet o)]
>(reduce (fn [m [^String k v]]
>  (assoc m (keyword k) (->clj v)))
>{} entries)))
>
>   java.util.List
>   (->clj [o] (vec (map ->clj o)))
>
>   java.lang.Object
>   (->clj [o] o)
>
>   nil
>   (->clj [_] nil))
>
> (defn as-clj-map
>   [m]
>   (->clj m))
>
>
> Let me know if this works for you & meets your performance requirements.
>
> Regards,
> BG
>
> -- 
> Baishampayan Ghose
> b.ghose at gmail.com
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/30c8edaf-bafe-4e78-9503-430d9f2c85ef%40googlegroups.com.


Re: Recursively convert Java Map to Clojure Map

2011-10-16 Thread Chouser
On Sun, Oct 16, 2011 at 1:51 AM, Baishampayan Ghose b.gh...@gmail.com wrote:
 On Sun, Oct 16, 2011 at 11:16 AM, Jestan Nirojan
 jestanniro...@gmail.com wrote:
 Your solution worked, about 4x~5x improvement.
 Thanks a lot BG.

 Glad that it worked for you, Jestan. Protocols are usually the right
 approach for these kind of tasks.

Why convert at all?  Java maps can be used quite conveniently in Clojure.

If it's because you need the map to be persistent, you might consider
converting each map only when the user attempts to conj or assoc,
rather than doing all the work up front.  Since those aren't (yet!)
protocols, I supposed you'd have to either use a non-standard conj and
assoc, or create a wrapper around the Java Maps.  Of course there are
complications with all three of these solutions, so you'll have to
choose carefully based on your needs.

--Chouser

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Recursively convert Java Map to Clojure Map

2011-10-15 Thread Baishampayan Ghose
 I have a Java Map contains Map of Maps/List (JSON like map) and have
 to convert to Clojure map (This happens at Java - Clojure Interop), So
 I have written a converter function 'as-clj-map' by modifying the
 clojure walk functions, It works fine, but consume lot of cpu when the
 data structure is quit big. Any suggestions to improve this code?

What about using protocols for this job?

(defprotocol ConvertibleToClojure
  (-clj [o]))

(extend-protocol ConvertibleToClojure
  java.util.Map
  (-clj [o] (let [entries (.entrySet o)]
   (reduce (fn [m [^String k v]]
 (assoc m (keyword k) (-clj v)))
   {} entries)))

  java.util.List
  (-clj [o] (vec (map -clj o)))

  java.lang.Object
  (-clj [o] o)

  nil
  (-clj [_] nil))

(defn as-clj-map
  [m]
  (-clj m))


Let me know if this works for you  meets your performance requirements.

Regards,
BG

-- 
Baishampayan Ghose
b.ghose at gmail.com

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Recursively convert Java Map to Clojure Map

2011-10-15 Thread Jestan Nirojan
Your solution worked, about 4x~5x improvement.
Thanks a lot BG.

regards.
-Jestan Nirojan



On Oct 15, 2:10 pm, Baishampayan Ghose b.gh...@gmail.com wrote:
  I have a Java Map contains Map of Maps/List (JSON like map) and have
  to convert to Clojure map (This happens at Java - Clojure Interop), So
  I have written a converter function 'as-clj-map' by modifying the
  clojure walk functions, It works fine, but consume lot of cpu when the
  data structure is quit big. Any suggestions to improve this code?

 What about using protocols for this job?

 (defprotocol ConvertibleToClojure
   (-clj [o]))

 (extend-protocol ConvertibleToClojure
   java.util.Map
   (-clj [o] (let [entries (.entrySet o)]
                (reduce (fn [m [^String k v]]
                          (assoc m (keyword k) (-clj v)))
                        {} entries)))

   java.util.List
   (-clj [o] (vec (map -clj o)))

   java.lang.Object
   (-clj [o] o)

   nil
   (-clj [_] nil))

 (defn as-clj-map
   [m]
   (-clj m))

 Let me know if this works for you  meets your performance requirements.

 Regards,
 BG

 --
 Baishampayan Ghose
 b.ghose at gmail.com

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Recursively convert Java Map to Clojure Map

2011-10-15 Thread Baishampayan Ghose
On Sun, Oct 16, 2011 at 11:16 AM, Jestan Nirojan
jestanniro...@gmail.com wrote:
 Your solution worked, about 4x~5x improvement.
 Thanks a lot BG.

Glad that it worked for you, Jestan. Protocols are usually the right
approach for these kind of tasks.

Regards,
BG

-- 
Baishampayan Ghose
b.ghose at gmail.com

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en