#clojure log - Jun 06 2008

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

12:27 dudleyf: rhickey: If you get a second:

12:27 http://github.com/dudleyf/clojure-bin/tree/master/clojure

12:28 It's a little script to run Clojure programs

12:29 rhickey: dudleyf: Thanks. I'm actually a bad candidate for evaluating this, being the producer rather than a consumer of Clojure. Why not post to the group and start a discussion?

12:31 dudleyf: Will do.

12:32 arbscht: dudleyf: it could be helpful if one could switch the path to the java binary

12:42 dudleyf: arbscht: I was going to make it relative to JAVA_HOME

12:43 arbscht: dudleyf: that could work

12:46 dudleyf: rhickey: I know it's come up before, but what are your current thoughts about adding string interpolation?

12:46 rhickey: dudleyf: I'm not opposed, depending on how it's done

12:59 cgrand: thanks for helping out on the group

13:02 Leafw: did doubles/floats et al help out with your macro?

17:51 slava: hi rhickey!

17:54 rhickey: hi

18:05 Lau_of_DK: Guys, I suppose you've all seen Richs (def fib-seq) on the Wiki, which seeds 0 1 and then returns a lazy list of fibs with some type of caching. Is there anyway to do a similar (def primes) or do I have to use (defn) and make a reqular function?

18:13 scgilardi: fib-seq defines a function and then evaluates it to produce the sequence lazily. It could have been done in two steps with an explicit helper function. This page (at the bottom) has an implementation of a lazy list of primes in common lisp that looks like it would be easy to translate to clojure. Is your goal to produce a lazy sequence of primes? http://www.lassila.org/blog/archive/2006/05/fun

18:13 _with_lazy_e_1.html

18:13 http://www.lassila.org/blog/archive/2006/05/fun_with_lazy_e_1.html

18:14 Lau_of_DK: Looking at the page now, but yea it was 2 part, 1) produce a lazy list of primes , 2) do it as a (def) because as I understood the wiki it gave some advantages like cached results

18:16 scgilardi: the caching of results comes from using lazy-cons which can be done in or out of a defn.

18:16 Lau_of_DK: ok, good

18:18 "Name a sequence that is seeded with [0 1] and forego the function so that instead of starting over with each call, you get a "cached infinite seq, lazily extended."

18:18 A bit confusing since both examples use lazy-cons

18:18 no wait, of course the function calculates from scratch when its called

18:20 scgilardi: right, so it is significant that it's a def. the lazy sequence stays around across accesses of the sequence. cool.

18:20 the caching part is mentioned in (doc lazy-cons)

18:20 Lau_of_DK: k, so I gotta work out a def primes for it to be effective

18:24 scgilardi: seems right to me

19:07 Lau_of_DK: Hmm - I need brains, check this out

19:08 (defn prime?

19:08 [n]

19:08 (every? (fn [x] (> (rem n x) 0)) (range 2 n)))

19:08 (def primes

19:08 (concat [1 2]

19:08 ((fn rprime [x]

19:08 (let [x1 (+ x 1)

19:08 x2 (+ x 2)]

19:08 (lazy-cons (if (prime? x) x (rprime x1))

19:08 (if (prime? x1) x1 (rprime x2))))) 3)))

19:08 user> (take 5 primes)

19:08 (1 2 3 5 7)

19:08 So far so good right? but if I -take- more than 5 primes, it throws a StackOverFlow exception...?

19:17 scgilardi: (defn sieve [s]

19:17 (lazy-cons (first s)

19:17 (sieve (filter

19:17 (fn [x]

19:17 (not

19:17 (= (rem x (first s)) 0)))

19:17 (rest s)))))

19:17 (defn primes []

19:17 (sieve (iterate inc 2)))

19:17 that's from lou franco's blog.

19:18 (defn sieve [s]

19:18 (lazy-cons (first s)

19:18 (sieve (filter

19:18 #(not (= (rem % (first s)) 0))

19:18 (rest s)))))

19:18 (defn primes []

19:18 (sieve (iterate inc 2)))

19:18 Lau_of_DK: Its a different approach, but if def caches the results, by principle it should not overflow ever right?

19:18 scgilardi: actually the latter is what I'm using. it runs out of heap space someplace between 1000 and 10000.

19:19 Lau_of_DK: Hmm... This is a little weird, I've never had any trouble doing such computations in SBCL

19:19 scgilardi: well the heap does have a fixed size, but I don't think we're near that. (the size is tunable in the java command line)

19:20 I'm having trouble turning this all into something that can use "recur" to avoid stack overflow, but this heap overflow is a different issue.

19:21 Lau_of_DK: Ok

19:22 I'm a bit weak on that side of understanding the mechanics of the compiler

19:31 scgilardi: with this

19:32 (defn sieve [s]

19:32 (lazy-cons (first s)

19:32 (sieve (filter

19:32 #(not (= (rem % (first s)) 0))

19:32 (rest s)))))

19:32 (def prime-seq

19:32 (lazy-cons 2 (sieve (iterate #(+ % 2) 3))))

19:32 and a 256 MB heap I'm able to get 1900 primes before I run out of heap space

19:33 Lau_of_DK: Sounds good - How did you set the heap to 256 megs?

19:33 scgilardi: with a 512 MB heap, I'm able to get 2000, but not 3000.

19:34 These are the memory related options for the "java" command line that launches clojure "-Xms32M -Xmx256M"

19:34 ms is the initial heap size and mx is the max

19:34 Lau_of_DK: ah, thats useful, thanks alot

19:34 scgilardi: you're welcome

Logging service provided by n01se.net