#clojure logs

2008-02-27

10:24ChouserI accidentally left out the ":else" or "true" before my final case in (cond ...) and it worked correctly anyway.
10:26rhickeyI need to add a check for even args, but I don't think it works generally:
10:26rhickeyclojure=> (cond nil 1 2)
10:26rhickeynil
10:26rhickeyclojure=> (cond nil 1 :else 2)
10:26rhickey2
10:29Chouserright, I was about to type that and got distracted.
10:30ChouserIt'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:53Chouserzip/next and zip/end can't be used to walk a sub-tree, can they?
10:55rhickeyno - you can create a zipper on the sub-tree though
10:56Chouserbut then I can't navigate from some walked-to node to anywhere outside the sub-tree.
10:56Chousers'okay -- I'll make this work, and then see if what I'm doing is just too weird. ;-)
14:13ChouserHow can I tell if an object is a zip loc vs. some other kind of seq?
14:15ChouserHm, check its meta-data for branch?
14:41ChouserIs it evil to use ((meta rtn) :zip/branch?) to distinguish between a loc and some other seq?
14:43rhickeyyes
14:44rhickeybut you can _add_ your own meta to the location object returned by zipper...
14:45rhickeyand it should propagate to all other locations you get to from there
14:47Chouserhm.
14:48Chouserso you don't think the checking of metadata is evil, but me checking for your metadata is.
14:51rhickeyright, because I might change it
14:52rhickeynote that those keywords are namespace-qualified
14:53rhickeyPerhaps I could provide a zipper? predicate, in which I check my own metadata
14:53Chouserok, makes sense to me.
14:56ChouserI'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:58rhickeyI don't think anyone has - I haven't, but I've needed metadata often enough that I put it in Clojure on spec
15:22Chouserto add {:foo bar} to html's meta: (meta (with-meta html (assoc (meta html) :foo :bar)))
15:24rhickeyI don't get the outer meta - (with-meta html (assoc ^html :foo :bar)) should do it
15:25rhickeyalso, if you are adding metadata to someone else's stuff, best to use qualified keywords
15:25rhickey:my/foo
15:34Chouseroh, sorry, the outer was so I could see it afterwards.
15:34rhickeyah
16:32ChouserWell, 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:33rhickeywas it easier?
16:33Chouserno, but now I can navigate as well.
16:33Chouserbefore I was losing my node context, so after filtering I only had the nodes that matched.
16:34ChouserNow I can find a node, and then say "right".
16:34rhickeyalso path
16:35ChouserI don't have a succint example, but the feel of it for me is definitely on par with xpath.
16:35ChouserAnd occsionally better, because I can drop in a #(... % ...) and do whatever I want.
16:36ChouserSo 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:36Chouser(xml-> html flatten :tr #(= (attr % :class) "sBlackText"))
16:37ChouserThat gives me a lazy seq of all <tr class="sBlackText"> nodes.
16:37rhickeyas locations?
16:37Chouserright.
16:37rhickeycool
16:38Chouserso then if I use map to put each of those in tr, I can say:
16:38Chouser(xml-> tr right flatten :tr [:td :font :b] flatten node string?)
16:39Chouserthat 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:40Chousersomething like value(./following-sibling::tr//tr[td/font/b]) in xpath
16:41Chouseranyway, it's working pretty well for me. So thanks for doing most of the work, rhickey!
16:41ChouserGotta go...
17:18NarayanS(filter (fn [[k v]](= (node-attrs k) v)) attr-values)
17:18NarayanSI am tryng to write it simpler
17:18NarayanS(filter #(= (node-attrs %1) %2) attr-values)
17:18NarayanSthis doenst work - destructure bind
17:18NarayanS(filter #(= (node-attrs (key %1)) (val %1)) attr-values)
17:18NarayanSthis works
17:18NarayanSam i doing something wrong?
17:31hoeck_is attr-values a list, vector or a hashmap?
17:32NarayanS(def attr-values {:cnty "66"})
17:33NarayanS(def node-attrs {:CovFlg "N", :cnty "66"})
17:35hoeck_ (filter #(= (node-attrs %1) %2) attr-values)
17:36hoeck_mhh
17:37NarayanSI get error when i do this filter code
17:37NarayanSjava.lang.IllegalArgumentException: Wrong number of args passed
17:37hoeck_which code?
17:38NarayanS (filter #(= (node-attrs %1) %2) attr-values)
17:38hoeck_yes, you have to use destructuring
17:39hoeck_so #(.. wont work
17:39NarayanS(filter (fn [[k v]](= (node-attrs k) v)) attr-values) this works
17:40hoeck_and you want a shorte way to do that?
17:40NarayanS(filter #(= (node-attrs (key %)) (val %)) attr-values) this works. i was just wondering if we have a destructuring in the #(
17:40hoeck_no, #( is without destructuring
17:41NarayanSthanks for answering
17:45hoeck_you're welcome
23:57jonathan_I just posted up a little SparkLine generator in Clojure. It's at http://jonathanwatmough.com/?p=220