#clojure log - Aug 22 2008

The Joy of Clojure
Main Clojure site
Google Group
IRC
List of all logged dates

10:36 Chouser: drewr: did you solve your XML problem?

11:00 drewr: Chouser: Yeah, I used the PullParser. It's awesome!

11:00 Chouser: oh, good!

11:03 drewr: Thanks for pointing me in that direction.

11:03 Chouser: you're quite welcome. Once you've used that, it's a bit amazing what a mess the SAX API is, isn't it?

11:15 drewr: I haven't done any SAX in a while, but I remember it taking me much longer to do something useful.

11:15 This time it might have been the parser, or just Clojure. :-)

11:30 Chouser: :-)

11:31 Yeah, using Clojure and proxy is by far the most pleasant SAX experience I've had.

11:44 * drewr is woefully behind on reading list messages

13:42 drewr: Can you not destructure a map into its k/v pair?

13:42 I thought something like this would work:

13:42 (let [{k v} {:foo :bar}] [k v]) => [:foo :bar]

13:43 I should say a map *entry*.

13:53 shoover1: map binding forms are intended to pull out values when you specify the keys. to get keys you would have to call seq on the map and then use vector binding forms like (let [[pair] (seq {:foo :bar})] pair)

14:02 drewr: In this case, I don't know what the keys are.

14:03 (map (fn [{k v}] v) {:foo 1 :bar 2 :baz 3}) => [1 2 3]

14:03 That's the behavior I want to mimic.

14:03 Obviously, there I could use (vals ...), but I'm focusing on the destructuring in the fn.

14:11 shoover: Do you care what the keys are? If so it seems like you can get the pairs pretty cleanly with (seq {...}). If not, (vals ...) supports first, second, nth, rest, and so on.

14:11 Either of those cases would support the sequence functions or further vector destructuring

14:13 Chouser: yes, you can destructure maps.

14:14 (let [{f1 :foo, b1 :bar} {:foo 1, :bar 2}] (list f1 b1))

14:14 drewr: Chouser: I don't know the keys are :foo and :bar.

14:14 Chouser: oh ... you want all the values?

14:15 drewr: I really want the key and value out of a map entry.

14:15 Chouser: how would you know which value goes with which key?

14:15 out of a single entry?

14:15 drewr: Yes.

14:16 Chouser: So like (def e (first {:foo 1})), and you want to destructure e?

14:16 drewr: Yes.

14:16 Chouser: (let [[k v] e] (list k v))

14:17 drewr: I was trying that on a map, not a map entry. Whoops.

14:17 Thanks.

14:18 I was wondering why I wasn't getting a vec.

14:19 Chouser: :-) np.

14:20 drewr: Is there a built-in function which recursively takes a PersistentMap and will convert them to java.util.HashMaps?

14:21 I'd give an example but I'm not sure how to express a HashMap symbolically.

14:21 Chouser: I don't even think there's one that does it for the first level.

14:22 drewr: I've built up a database record which ends up being a map that can have other maps as values.

14:22 Chouser: I think that came up on the forum recently -- a couple examples of copying into a java Map, and one that wrapped the PersistentMap in a proxy.

14:22 drewr: I need to normalize that to standard Java collections so I can talk to some other APIs.

14:22 OK, I'll check the list.

14:23 Chouser: sounds like you're in for some "reduce" fun, or maybe into+recur or something.

14:24 drewr: Yup :-)

16:05 lisppaste8: DrewR pasted "PersistentHashMap to HashMap" at http://paste.lisp.org/display/65710

16:07 Chouser: (instance? clojure.lang.PersistentArrayMap v)

16:08 looks good, though.

16:09 you get a call stack as deep as your maps are nested.

16:14 drewr: I'm assuming the nesting won't be but 2 or 3 levels.

16:14 Chouser: sure

16:15 The maps would have to be nested to a pretty masachistic depth for it to be a problem.

16:16 drewr: I'm doing this to get stuff into JSON. It seems to be working well.

16:17 Chouser: using some stock Java JSON writer?

16:17 I think someone mentioned writing a JSON writer on the forum, but I don't remember seeing any code.

16:20 drewr: The lib I'm using on top of net.sf.json.JSONObject can take a java.util.Map to populate the record.

17:46 ozzilee: Can someone tell me why I get an UnsupportedOperationError: foo with this genclass code?

17:46 Sorry, "UnsupportedOperationError: bar".

17:47 Er, rather: "Exception in thread "main" java.lang.UnsupportedOperationException: bar"

17:47 There :-)

17:48 Chouser: can you paste your genclass code?

17:48 lisppaste8: ozzilee pasted "genclass problem" at http://paste.lisp.org/display/65714

17:49 ozzilee: Heh sorry, forgot to hit the paste button.

17:49 It's friday.

17:50 lisppaste8: ozzilee annotated #65714 with "Should take [this], no help though" at http://paste.lisp.org/display/65714#1

17:51 Chouser: yeah, that was my first attempt. :-/

17:52 ozzilee: What did I do wrong?

17:53 Oh, nm, you meant adding [this] was your first attempt.

17:53 Chouser: yeah, sorry.

17:54 Well, bar is definitly a method of the class, as indicated both by the error and reflection.

17:55 ozzilee: Yeah. I looked in the genclass source, but it throws the same exception in a few different places, so I'd have to make some changes to figure out exactly where it comes from.

17:55 Also, 200-line functions hurt my brain.

17:56 * Chouser nods

17:59 Chouser: It's acting like Foo-bar is misnamed.

18:01 ozzilee: Ah, found it.

18:01 (in-ns 'com.ozzilee.Foo) should be (in-ns 'com.ozzilee)

18:03 Thanks for the help.

18:03 Chouser: ah -- beat me

18:03 by 2 minutes.

18:03 :-)

18:03 ozzilee: Heh. I copied the example from the source and started working backwards.

18:04 Chouser: I started adding debugging output to genclass and worked forward. Inferior.

18:05 ozzilee: Heh. Well it's 5:00 and I'm already overtime for the week, I'm going to take off. Thanks again.

21:07 Is there a way to define a function when you don't know the name of the function until run-time?

21:11 drewr: Why can't you use an anonymous fn?

21:12 ozzilee: I'm using genclass to create a class with a gensym'd name.

21:13 From what I understand the only way to create the methods for the class are with defn's.

21:13 drewr: I haven't used genclass enough.

21:17 ozzilee: Hmm, I'll have to rethink my approach.

22:07 Anyone know how to call a super method from a genclass'd method?

Logging service provided by n01se.net