Re: breaking early from a tight loop

2009-06-14 Thread Laurent PETIT
Hi, 2009/6/14 CuppoJava patrickli_2...@hotmail.com: I actually really do like the reduce with early exit abstraction. Because ultimately, that's what the question is. It's a reduce with optimization. However, I feel that Laurence's reduce is a little too specific. The early exit condition

Re: breaking early from a tight loop

2009-06-14 Thread Laurent PETIT
2009/6/14 James Reeves weavejes...@googlemail.com: On Jun 13, 9:57 pm, Laurent PETIT laurent.pe...@gmail.com wrote: The filter and map functions produce lazy seqs, so the sequence is only walked once. Well, isn't walking 3 different sequences 1 time almost equivalent (in terms of

Re: breaking early from a tight loop

2009-06-14 Thread Sudish Joseph
On Jun 13, 4:17 pm, Laurent PETIT laurent.pe...@gmail.com wrote: So it really seems to me that the missing abstraction, here, is being able to do a reduce over the list of pixels, and being able, from the reduction function, to quit the reduction early. A lazy right fold[1] allows

Re: breaking early from a tight loop

2009-06-14 Thread Sudish Joseph
On Jun 13, 4:39 pm, Laurent PETIT laurent.pe...@gmail.com wrote: Hi, Well, the array is iterated once by map, the new seq created by map is iterated once by filter, and the new seq created by filter is iterated once by count, so right, I should have written : 3 walks of seqs of the size of

Re: breaking early from a tight loop

2009-06-14 Thread Max Suica
Laurent, I think this is a close variant of the early exiting reduce function you proposed: (defn reduce-bail [f pred? val col] (let [s (seq col)] (if s (if (pred? val) (recur f pred? (f val (first s)) (next s)) val)

Re: breaking early from a tight loop

2009-06-14 Thread Max Suica
On Jun 14, 5:04 am, Max Suica max.su...@gmail.com wrote: (defn interesting? [pixels in-range? count]   (let [p-count (reduce-bail (fn [c p] (if (in-range? p) (inc c) c)) (partial count) 0 pixels)]   [( = count yummy-pix-count) p-count])) Shoot: s/[( = count yummy-pix-count) p-count]))/[( =

Re: breaking early from a tight loop

2009-06-14 Thread Max Suica
A lazy right fold[1] allows short-circuiting, so here's one attempt: Wow, that made my head explode. Some points: 1) That's not exatly foldr, as (foldr + 0 [range 100]) ought to work 2) Foldr is not tail recursive nor can you really call an anamorphism lazy 3) Never the less you did it,

Re: breaking early from a tight loop

2009-06-14 Thread Max Suica
I'm sorry, folds are catamorphisms, while stuff like (repeatedly f) or (repeat n x) are anamorphisms, and can certainly be lazy. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group,

Re: breaking early from a tight loop

2009-06-14 Thread Sudish Joseph
On Jun 14, 5:31 am, Max Suica max.su...@gmail.com wrote: A lazy right fold[1] allows short-circuiting, so here's one attempt: Wow, that made my head explode. Some points: 1) That's not exatly foldr, as  (foldr + 0 [range 100]) ought to work Agreed, having to write that as (foldr (fn [x

Re: super-lazy-seq

2009-06-14 Thread Jarkko Oranen
On Jun 14, 6:37 am, Wrexsoul d2387...@bsnow.net wrote: On Jun 13, 11:07 pm, James Reeves weavejes...@googlemail.com wrote: For instance, lets say I want to return a lazy list of all the lines in all the files in a directory tree:   (use '(clojure.contrib java-utils                      

Re: Clojure equivalent to Ruby's ERB

2009-06-14 Thread markgunnels
I just wanted to report back that StringTemplate proved to be the perfect solution. I also wanted to recommend Terrence Parr's (the creator and ANTLR and StringTemplate) new book Language Design Patterns from The Pragmatic Programmers for anyone doing parsing and code generation. It is an

Re: super-lazy-seq

2009-06-14 Thread James Reeves
On Jun 14, 4:37 am, Wrexsoul d2387...@bsnow.net wrote: Seems to me that unless you completely consume the sequence, it will leak a file handle. That's true, but that's a problem that affects all seqs. There's no current way to mark a seq that comes from a stream as being discarded or closed,

Performance Penalty Converting from Java Code

2009-06-14 Thread tmountain
I've been playing around with rewriting some Java code in Clojure and did some simple benchmarking in the process. In this case, there's a huge disparity in the performance numbers between the two languages, and I'm wondering what the cause may be. The program rotates a string from , aaab,

Re: Primitive char Type

2009-06-14 Thread James Reeves
On Jun 14, 4:40 am, Wrexsoul d2387...@bsnow.net wrote: What I miss is foo-array for foo not in #{int long float double}, particularly for (= foo byte). You can use (make-array Byte/TYPE size) and (into-array Byte/TYPE byte- coll). - James --~--~-~--~~~---~--~~

Re: Primitive char Type

2009-06-14 Thread James Reeves
On Jun 14, 3:28 am, tmountain tinymount...@gmail.com wrote: java.lang.IllegalArgumentException: No matching method found: setCharAt for class java.lang.StringBuilder (NO_SOURCE_FILE:0) user= (type (char \a)) java.lang.Character ; should be char? You could try: (.charValue \a) - James

Re: Primitive char Type

2009-06-14 Thread tmountain
That doesn't work either. It appears this isn't an issue with Java 6, but that doesn't help me on my PPC powerbook, which is apparently stuck with the Java 5 JRE for the foreseeable future. Thanks, Travis On Jun 14, 10:44 am, James Reeves weavejes...@googlemail.com wrote: On Jun 14, 3:28 am,

Re: Performance Penalty Converting from Java Code

2009-06-14 Thread Parth
On Jun 14, 7:00 pm, tmountain tinymount...@gmail.com wrote: I've been playing around with rewriting some Java code in Clojure and did some simple benchmarking in the process. In this case, there's a huge disparity in the performance numbers between the two languages, and I'm wondering what

Re: super-lazy-seq

2009-06-14 Thread Wrexsoul
On Jun 14, 9:39 am, James Reeves weavejes...@googlemail.com wrote: Okay, but don't underestimate the power of higher level functions. I don't know whether it would apply to your code, but the repeatedly function can be used to create a lazy seq from a function with side- effects. For

Re: catching exception error

2009-06-14 Thread peg
Thanks for your answers. I read the thread and understood why I don't get my IllegalArgumentException but a new RuntimeException that wrapps it. But I don't understand what the reason is to create a new RuntimeException (stack tracing?) knowing that IllegalException is a subclass of

Re: Performance Penalty Converting from Java Code

2009-06-14 Thread Emeka
kedu Travis, (defn base26 [ n] (let [seed-string s (new StringBuilder seed-string)] (loop [ pos (- (count seed-string) 1) x n] (if (and ( pos 0)( x 0)) (do (. s setCharAt pos (char (+ (int \a) (mod x 26 (recur (- pos 1) (/ x 26) (. s

Re: super-lazy-seq

2009-06-14 Thread James Reeves
On Jun 14, 6:32 pm, Wrexsoul d2387...@bsnow.net wrote: I wrote super-lazy-seq because repeatedly can't generate a finite sequence. It just spat out (File1 File2 File3 File4 nil nil nil nil nil nil nil ... Well, you could wrap it in take-while: (defn custom-lazy-seq [stream]

Re: Clojure equivalent to Ruby's ERB

2009-06-14 Thread Emeka
markgunnels, Have you used clojure and StringTemplate to do something? If so, I would like to tap your knowledge there. Regards, Emeka On Sun, Jun 14, 2009 at 1:36 PM, markgunnels markgunn...@gmail.com wrote: I just wanted to report back that StringTemplate proved to be the perfect

Arraylist faster than primitive array?

2009-06-14 Thread evandi
If I run this code: (def ibounds 100) (let [arlist (new java.util.ArrayList ibounds)] (dotimes [i ibounds] (.add arlist 0)) (time (dotimes [x 100] (dotimes [i ibounds] (.set arlist i 1) (let [arlist (make-array Integer/TYPE ibounds)]

dosync ref-set and threads coordination problem

2009-06-14 Thread vmargioulas
In the example below 256 clients threads put a byte (0-255) to server- socket reference var items, then connect to server-socket, write the same byte to socket stream and close the connection. The server connections threads reads a byte from the socket stream and remove it from the var items. At

Re: Primitive char Type

2009-06-14 Thread Stephen C. Gilardi
On Jun 13, 2009, at 10:46 PM, Stephen C. Gilardi wrote: user= (.setCharAt s 0 \c) java.lang.IllegalArgumentException: Can't call public method of non- public class: public void java.lang.AbstractStringBuilder.setCharAt(int,char) (NO_SOURCE_FILE:0) I'm not sure why. StringBuilder extends

Re: super-lazy-seq

2009-06-14 Thread Wrexsoul
On Jun 14, 2:53 pm, James Reeves weavejes...@googlemail.com wrote: On Jun 14, 6:32 pm, Wrexsoul d2387...@bsnow.net wrote: I wrote super-lazy-seq because repeatedly can't generate a finite sequence. It just spat out (File1 File2 File3 File4 nil nil nil nil nil nil nil ... Well, you