#clojure log - Sep 22 2008

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

9:50 rhickey: I've added print-method multimethod and switched printing to use it

9:50 built on contribution from Erik Soehnel

9:55 wwmorgan: rhickey: that's a lot cleaner than the old way of doing it

9:56 also I think it should recognize Patterns and print them using the #"foo" form

9:56 rhickey: wwmorgan: right, it needed the isa multimethod enhancements I added this summer

9:56 wwmorgan: ah yes, was waiting for this to do patterns...

10:02 wwmorgan: I've added print-method for java.util.regex.Pattern

10:02 wwmorgan: rhickey: sweet

11:26 Chouser: excellent, now all I need to do is implement MultiFn in JS, and printing should work.

11:27 abrooks: Chouser: Great work!

13:42 jgracin: rhickey: I see that 1040 revision has fixed print-method exception. However, I'm still get the following when compiling code wlth failures, from Slime: ...(incoming paste)

13:42 lisppaste8: jgracin pasted "Compiler exception" at http://paste.lisp.org/display/67278

13:47 rhickey: jgracin: there's too much going on there for me to suss this out - can you narrow it down at all?

13:47 jgracin: rhickey: I'll try.

13:57 rhickey: it seems to me that during compilation, LazyCons.first is called and it throws RuntimeException, and this exception is not encapsulated in CompilerException, which is what Slime expects. Any ideas? I'll keep digging...

13:58 rhickey: seems to me Slime shouldn't hardwire CompilerException

14:03 leafw: I have a conceptual problem. I am far too used to looping; suppose I have a list of objects, of which I have to find 3, given 3 desired names. I.e. getTitle has to be called on each object. What I would like to get back is a table of name - object. What is a good strategy in clojure to do that?

14:05 somwhow, is not obvious to me. I could do it perhaps with a recursive function that returns a table with a new entry, that table being given as argument ad thus extended, or nothing when reaching the end of the list.

14:09 ozzilee: leafw: You could do it with reduce: (reduce (fn [a x] (if (is-something-we-want (.getTitle x)) (assoc a x (.getTitle x) a) {})

14:09 cemerick: leafw: given a seq of those objects, this would give you a map of name ==> object: (apply hash-map (mapcat #(list (.getTitle %) %) seq-of-objects))

14:10 ozzilee: leafw: Where is-something-we-want is whatever determines whether that title is the one you want.

14:11 wwmorgan: leafw: here is one way to do it: http://paste.lisp.org/display/67282

14:11 leafw: thanks people, let me digest it

14:12 cemerick: I often end up using filter and map instead of reduce for stuff like that -- I've never liked the position of the first value in the 3-arg version of reduce.

14:13 ozzilee: cemerick: I prefer fold myself, as in (fold fn initial coll). But meh :-)

14:18 leafw: ozzilee: your pasted solution is missing parentheses...

14:19 cemerick: your solution looks really cool -- will it accept any list, like a java.util.ArrayList ? Or does it need a clojure list?

14:19 ozzilee: leafw: That's possible.

14:19 leafw: wwmorgan: thanks for that post, just got nice insight into memfn

14:20 It's amazing there are so many solutions

14:20 cemerick: leafw: seq-of-objects can be anything that's seq-able -- all collections can be treated as seqs

14:20 ozzilee: (reduce (fn [a x] (if (is-something-we-want (.getTitle x))) (assoc a x (.getTitle x) a) {}) ; Better.

14:22 leafw: thank you

14:23 hum, ozzilee : the 'if' has no body as you declared it.

14:26 ozzilee: Yeesh, writing lisp without emacs doesn't work out so well. This should be correct, sorry about that: (reduce (fn [a x] (if (is-something-we-want (.getTitle x)) (assoc a x (.getTitle x)) a)) coll {})

14:28 leafw: ozzilee: so you are doing: reduce fn list1 set1 , is that right?

14:29 ozzilee: (reduce function seq initial-value), yeah.

14:29 leafw: aha

14:29 intial-value ... got it, got it.

14:29 ozzilee: We start with an empty hash-map, and then assoc in x if it's something we want, otherwise we return what we've reduced so far (a).

14:30 leafw: I understand it. Thanks.

14:31 ozzilee: Ok. Next time I'll check my parens before I post :-)

14:31 rhickey: (into {} (for [x coll :when (we-like x)] [(key-for x) (val-for x)]))

14:32 cemerick: hrm, forgot about into...

15:48 leafw: for all clojure's power, debugging is one of its weaknessess: the stack traces never mean much to me ...

15:48 abrooks: leafw: There's been several threads about this on the Clojure group.

15:49 leafw: And I have to agree.

15:49 TreeRex: I found it just takes practice.

15:49 abrooks: If I get things right, it just works. If I screw up I get to stare blankly at the stack trace and flail around until I figure out what's wrong.

15:50 leafw: problem is, when an exception is thrown on code pasted to an interpreter, it doesn't give back anything useful such as the names of the function where it failed, and its parent stack of caller functions.

15:50 abrooks: It seems that with all the introspective abilities, we should be able to provide much more useful information.

15:51 leafw: that alone would help a lot, much more than (defn pdatas (reduce (fn [project ptable] (assoc ptable project (create-pdata project)) ptable) (ControlWindow/getProjects) {}))

15:51 abrooks: leafw: By interpreter you mean REPL?

15:51 leafw: oops

15:51 bad paste (we luve linux pasting)

15:51 yes, REPL

15:56 at least, functional-style makes things trivial to debug manually

15:56 but I can't imageine what it will be to debug a large project.

15:56 even making on-site test functions is trivial and almost natural.

15:57 abrooks: If C compilers can print out the offending line with a "------^ " pointing to the offending expression / token it seems that Clojure should be able to as well.

15:58 leafw: I wish I knew how. I may dig in one of this days, into Clojure's exception handling system. With all the metadata, there must be a way to just print the stack of functions.

15:59 ozzilee: leafw: I actually hacked that in the other day on my lunch break, it shouldn't be too tough to do it nicely.

16:01 lisppaste8: ozzilee pasted "chicken's errors" at http://paste.lisp.org/display/67284

16:01 ozzilee: fwiw that's how chicken scheme shows errors, which I always appreciated. Other lisps may do something similar.

16:03 abrooks: ozzilee: That's exactly what I'd like to se.

16:03 see

16:04 leafw: yeah, that's the right direction IMO

16:09 ozzilee: Should be do-able, but... "not it!" :-)

16:09 abrooks: ozzilee: :)

16:22 TreeRex: While I haven't used it, the screenshots of the Enclojure debugger look promising: perhaps there is some useful functionality in that IDE which could be moved into the REPL.

17:03 leafw: my problem with enclojure and the like is that its not an editor (aka it's not vim, it's not emacs)

17:03 I would use it only for debugging, if they make that worthwhile.

17:11 ozzilee: parth_m started a repl written in clojure, might be something worth looking at: http://code.google.com/p/iclj/

17:41 leafw: is there any built-in way to do a .toArray() to a list? Aka put all elements of a list into an array of a certain type. I can do that with a reduce, but I wonder if there are easier ways.

17:45 ozzilee: (into-array seq) ?

17:47 leafw: nice

17:47 thanks

18:36 and using reduce/map and the like, how can one add all values of a half-matrix? I.e. all values of an array NxM, where only values under the diagonal should be added.

18:44 rhickey_: leafw: try for

18:46 leafw: still te problem of a mutable var that accumulates the sum

18:47 rhickey_: the problem is in wanting one - just pick the values you care about first, then add them with reduce +

18:48 leafw: that means a huge memory increase -- which is fine in most occasions, but not in this one.

18:49 thanks anyway

18:49 rhickey_: um, no, since seqs can be lazy

18:49 leafw: if anything, there are integer indices to the arrays right?

18:49 rhickey_: for yields a lazy seq, for instance

18:49 range yields a lazy sequence

18:49 etc

18:50 leafw: I think I don't understand lazy seqs them

18:50 s/them/then

18:53 rhickey_: try (reduce + (range 100000000)) and watch memory use, it doesn't budge

18:54 leafw: meaning, each element of the sequence is returned without the sequence existing in memory?

18:55 rhickey_: right

18:56 a lazy seq is a recipe for a seq, the whole seq need never exist. As you call first/rest you get bits of it created on demand, as long as you don't hold onto them, they are discarded after you use them

18:56 cycle returns an infinite seq, for instance

18:56 you'll never use all of it

18:57 leafw: very nice. Now it's my task to figure out how to use it for the half-matrix sum.

18:58 rhickey_: try for

18:58 :)

19:04 leafw: so I need a lazy seq of lazy seqs right ?

19:04 but then I need to flatten it ... man, thinking this way is totally new

19:05 rhickey_: (for [ x (range 3) y (range 3)] [x y])

19:06 leafw: (for [x (range 10) y (range x)] [x y])

19:06 interesting

19:09 rhickey_: there's also :when and :while options

19:11 leafw: for a NxN matrix, the half-matrix is (for [x (range 0 N) y (range 0 x)] [x y])

19:11 the lower one, at least, without the diagonal of zeros.

19:48 ericthor: Has anything changed with arglists recently? If I say (meta #'clojure/*) I'm getting an IndexOutOfBoundsException when the REPL attempts to print the arglist

19:51 aking: ericthor: works here

19:52 ericthor: aking: what svn revision are u using?

19:52 aking: ericthor: 1039 - trying with 1040

19:53 ericthor: aking: could be time for a full rebuild on my end here...

19:53 aking: hmm.. doesn't work with 1040

19:54 ericthor: phew

19:54 thanks

19:54 It appears to be the 0th element

19:55 is said to be out of bounds

19:55 1 2 3 all ok of 4

19:55 I'll back up to 1039

19:56 aking: ericthor: it might have been 1038 - I had updated last night, but there looks to be 2 updates today

19:56 ericthor: aking: ok thanks...I'll try both

20:07 aking: ericthor: looks like I was at a slightly older version then I thought - anything after 1037 breaks

20:07 ericthor: aking: 1037 is oko?

20:07 ok?

20:07 aking: yup

20:08 ericthor: aking: I was just doing the same thing...saved me the trouble. Thanks!

20:34 rhickey_: fixed print-method on empty vector

20:35 rev 1041

21:30 ericthor: rhickey: Thanks!

21:30 rhickey_: sure, new print multimethods getting shaken out...

23:32 yangsx: what is the best way to use clojure.contrib? I'm trying clojure with the svn repo

Logging service provided by n01se.net