#clojure log - Feb 27 2008

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

10:24 Chouser: I accidentally left out the ":else" or "true" before my final case in (cond ...) and it worked correctly anyway.

10:26 rhickey: I need to add a check for even args, but I don't think it works generally:

10:26 clojure=> (cond nil 1 2)

10:26 nil

10:26 clojure=> (cond nil 1 :else 2)

10:26 2

10:29 Chouser: right, I was about to type that and got distracted.

10:30 It's that final odd expression is of course treated as a test, so the return value of cond may not be what you want.

10:53 zip/next and zip/end can't be used to walk a sub-tree, can they?

10:55 rhickey: no - you can create a zipper on the sub-tree though

10:56 Chouser: but then I can't navigate from some walked-to node to anywhere outside the sub-tree.

10:56 s'okay -- I'll make this work, and then see if what I'm doing is just too weird. ;-)

14:13 How can I tell if an object is a zip loc vs. some other kind of seq?

14:15 Hm, check its meta-data for branch?

14:41 Is it evil to use ((meta rtn) :zip/branch?) to distinguish between a loc and some other seq?

14:43 rhickey: yes

14:44 but you can _add_ your own meta to the location object returned by zipper...

14:45 and it should propagate to all other locations you get to from there

14:47 Chouser: hm.

14:48 so you don't think the checking of metadata is evil, but me checking for your metadata is.

14:51 rhickey: right, because I might change it

14:52 note that those keywords are namespace-qualified

14:53 Perhaps I could provide a zipper? predicate, in which I check my own metadata

14:53 Chouser: ok, makes sense to me.

14:56 I've never used a metadata system in a functional language like this. I have a long way to go before I'm comfortable with how it should be used.

14:58 rhickey: I don't think anyone has - I haven't, but I've needed metadata often enough that I put it in Clojure on spec

15:22 Chouser: to add {:foo bar} to html's meta: (meta (with-meta html (assoc (meta html) :foo :bar)))

15:24 rhickey: I don't get the outer meta - (with-meta html (assoc ^html :foo :bar)) should do it

15:25 also, if you are adding metadata to someone else's stuff, best to use qualified keywords

15:25 :my/foo

15:34 Chouser: oh, sorry, the outer was so I could see it afterwards.

15:34 rhickey: ah

16:32 Chouser: Well, I'm liking this quite a bit. I tweaked the mapcat pipeline thing I had working before so that now it operates on xml zippers instead of straing xml maps.

16:33 rhickey: was it easier?

16:33 Chouser: no, but now I can navigate as well.

16:33 before I was losing my node context, so after filtering I only had the nodes that matched.

16:34 Now I can find a node, and then say "right".

16:34 rhickey: also path

16:35 Chouser: I don't have a succint example, but the feel of it for me is definitely on par with xpath.

16:35 And occsionally better, because I can drop in a #(... % ...) and do whatever I want.

16:36 So the API is pretty close to what I want. The internals are pretty ugly 'cuz I don't really know what I'm doing. :-)

16:36 (xml-> html flatten :tr #(= (attr % :class) "sBlackText"))

16:37 That gives me a lazy seq of all <tr class="sBlackText"> nodes.

16:37 rhickey: as locations?

16:37 Chouser: right.

16:37 rhickey: cool

16:38 Chouser: so then if I use map to put each of those in tr, I can say:

16:38 (xml-> tr right flatten :tr [:td :font :b] flatten node string?)

16:39 that steps to then next <tr>, finds all descendant <tr>'s that have a <td><font><b></b></font></td> structure, and the extracts all the contained strings from that <tr> as a list.

16:40 something like value(./following-sibling::tr//tr[td/font/b]) in xpath

16:41 anyway, it's working pretty well for me. So thanks for doing most of the work, rhickey!

16:41 Gotta go...

17:18 NarayanS: (filter (fn [[k v]](= (node-attrs k) v)) attr-values)

17:18 I am tryng to write it simpler

17:18 (filter #(= (node-attrs %1) %2) attr-values)

17:18 this doenst work - destructure bind

17:18 (filter #(= (node-attrs (key %1)) (val %1)) attr-values)

17:18 this works

17:18 am i doing something wrong?

17:31 hoeck_: is attr-values a list, vector or a hashmap?

17:32 NarayanS: (def attr-values {:cnty "66"})

17:33 (def node-attrs {:CovFlg "N", :cnty "66"})

17:35 hoeck_: (filter #(= (node-attrs %1) %2) attr-values)

17:36 mhh

17:37 NarayanS: I get error when i do this filter code

17:37 java.lang.IllegalArgumentException: Wrong number of args passed

17:37 hoeck_: which code?

17:38 NarayanS: (filter #(= (node-attrs %1) %2) attr-values)

17:38 hoeck_: yes, you have to use destructuring

17:39 so #(.. wont work

17:39 NarayanS: (filter (fn [[k v]](= (node-attrs k) v)) attr-values) this works

17:40 hoeck_: and you want a shorte way to do that?

17:40 NarayanS: (filter #(= (node-attrs (key %)) (val %)) attr-values) this works. i was just wondering if we have a destructuring in the #(

17:40 hoeck_: no, #( is without destructuring

17:41 NarayanS: thanks for answering

17:45 hoeck_: you're welcome

23:57 jonathan_: I just posted up a little SparkLine generator in Clojure. It's at http://jonathanwatmough.com/?p=220

Logging service provided by n01se.net