#clojure logs

2008-06-07

02:01sohailhey, I don't know scheme but I know enough of CL to keep me dangerous. Is reading the reference enough for me or do I need to be a scheme geek to use clojure?
08:42cgrandLau_of_DK: if you are still interested in a primes seq: http://clj-me.blogspot.com/2008/06/primes.html
08:42Lau_of_DKOh I am :) Thanks
08:43Lau_of_DKIs that your own blog?
08:51Lau_of_DKcgrand, that is an amazing prime routine - probably the best Ive ever seen
08:52Lau_of_DKtakes 2500 primes in 0.033582 msecs
08:52cgrandthanks, that's what I thought when I see the haskell version (I lost the url)
08:54Lau_of_DKinsane, takes 5000 primes in 0.03---
08:54cgrandthe primes are cached
08:54Lau_of_DKI have to study it more closely soon as Im done with the task Im on - Look forward to some questions :)
09:03cgrandI can't guarantee I'll be there the whole afternoon
09:08Lau_of_DKIts ok, dont hang around for my sake, it'll be hours before I can sit down and focus on Clojure - But thanks alot for the link, its really helpful
09:11cgrandbtw when you do (time (take 2500 primes)) you don't compute 2500 primes (laziness is a virtue :-)) (time (dorun (take 2500 primes))) will force computation
09:11Lau_of_DKBut it outputs something that looks like 2500 numbers to the screen - they must have been computed
09:17cgrandif it outputs them then yes their are computed (or retrieved from cache). Here (time (take 2500 primes)) "Elapsed time: 0.038832 msecs" but outputs only "(". While (time (dorun (take 2500 primes))) "Elapsed time: 136.393034 msecs"
09:36blackdoghi, can someone shed some light on these comments for me, rhickey: using (JThis. ...) makes that pretty declarative too
09:36blackdog13:08 Chouse1: Yeah I converted all my (new...)s to (JFoo. ...)s this morning. I'm still pretty ambivalent.
09:37blackdogwhat's this syntax for java objects?
09:45hoeckblackdog: (JFoo. ..) means (new JFoo ..)
09:46hoeckblackdog: http://clojure-log.n01se.net/date/2008-06-03.html (search for "(new Foo)")
10:10slavarhickey: does all mutable state in clojure go through stm?
10:46rhickeyslava: no, there are 3 kinds of references that can change - one is transactional, one is atomic (actor-like) and one is thread-local
14:40Lau_of_DKcgrand, you here?
15:00Lau_of_DKGents, is clojure.org the only place that I can get documentation for this language?
15:07JamesIryBesides miscellaneous blog posts, that's about it.
15:08Lau_of_DKI've had a few instances now where I couldnt find documentation on function, like recur, where is it documented?
15:20Lau_of_DKAnybody here got some informative links that arent found from clojure.org? (regarding clojure that is)
15:27JamesIrySorry, I had to step away for awhile. Here's where recur is documented. http://clojure.sourceforge.net/reference/special_forms.html
15:28Lau_of_DKNo need to appologize, but thank you for the link :)
15:29Lau_of_DKI dont know if Im alone with this, but I have a little bit of a hard time adjusting my mutatable mind to functional programming- How did you guys embrace is? Do I need to take a trip down Haskell lane ?
15:29JamesIryLau_of_DK: I have the advantage of having learned Scheme and then Haskell first.
15:30Lau_of_DKYea thats gotta be an advantage
15:30JamesIryI don't think it's absolutely necessary to learn Haskell first, but it does have the advantage of pretty much forcing you to jump in head first
15:30Lau_of_DKI think Ive solved about 20% of all Euler challenge in single (loop) statements, so this is a change of mindset^3
15:30Lau_of_DKk
15:36JamesIryLau_of_DK: But another alternative is to work through excercises in SICP but use Clojure. 90% or more of the book avoids mutation.
15:48Lau_of_DKYea, its just that I just did that in SBCL, so its a daunting task
15:48Lau_of_DKJamesIry, do you know of any tutorials on how to get started with fn programming?
15:49JamesIryIf you did SICP in SBCL then you should be covered
15:49Lau_of_DKmaybe, but I dont feel covered. My first instinct is always to jump into some with sideeffects :)
15:52rsynnottLau_of_DK: use erlang or something for a bit; it sorta forces the issue :)
15:52JamesIryWell, I really liked "Haskell: The Craft of Functional Programming" http://www.cs.kent.ac.uk/people/staff/sjt/craft2e/. I wasn't quite as impressed by "The Haskell School of Expression."
15:53Lau_of_DKgah :) Maybe I'll take my own advice and try to form some sort of tutorial. I quite like the example where some guy ported Practical Common Lisp Chpt 3 to Clojure
15:53Lau_of_DKOk, thanks JamesIry, I'll check it out
15:53JamesIryThere ya go. Somebody else ported "The Reasoned Schemer" to Clojure
15:53JamesIryIf you do that you learn both functional and logic paradigms.
15:56Lau_of_DKSounds good
16:09Lau_of_DKuser> (nth primes 5)
16:09Lau_of_DK11
16:09Lau_of_DKuser> (take 5 primes)
16:09Lau_of_DK(1 2 3 5 7)
16:09Lau_of_DKIs this done on purpose?
16:14wybiralThink about indexing it, from 0
16:15wybiralvs taking 5 actual objects
16:15Lau_of_DKBut by that logic it should return 5, not 11 ?
16:15Lau_of_DKIts gone to index + 1, not zero-index
16:17ChouserI would recommend doing projecteuler problems, perhaps from the beginning, in clojure.
16:18Lau_of_DKChouser, Im already on it - though I didnt quite go back to the beginning
16:18wybiralLau, I don't get what you mean... 1 would be the 0th object
16:21Lau_of_DKnevermind, its the coffee talking :(
16:21Lau_of_DKYou're right, its just the zero-index thing
17:59Lau_of_DKuser> tst
17:59Lau_of_DK[:sum 1061897 :offset 5]
17:59Lau_of_DKhow do I access the 2 values in tst ?
18:03Chouser(:sum (apply hash-map tst))
18:04Chouser(nth tst 3) ; ==> 5
18:05Chouser(second tst) ; ==> 1061897
18:05Lau_of_DKThanks Chouser, thats exactly what I needed - In addition: #1 clue that you've spent to much time in Emacs: I wrote "th" and tabbed, expecting it to complete to "thanks" .. hehe :)
18:06Chouser;-)
18:06Chouser(let [{:keys [sum offset]} (apply hash-map tst)] [sum offset]) ; => [1061897 5]
18:25Chouserhm, I thought take-ns would be in boot.clj by now.
19:04Lau_of_DKWhat is [_] compared to [] ?
19:38Lau_of_DKI have a sequence of ints, which I need to compare numerically to a value, how do I cast the seq to int ? (similar to SBCLs parse-integer)
19:41JamesIryDoes't parse-integer attempt to parse a string to an integer?
19:41Lau_of_DKyea
19:41Lau_of_DK(def roofseq
19:41Lau_of_DK (fn [s roof]
19:41Lau_of_DK (loop [next 1]
19:41Lau_of_DK (if (> (apply int (first s)) roof)
19:41Lau_of_DK (rest s)
19:41Lau_of_DK (recur (conj s (nth s (inc next))))))))
19:41Lau_of_DKIts the (if) that I need to get working
19:45JamesIryI'm still not following. s is a sequence of ints, right?
19:45Lau_of_DKYes, I want to say that I want all the primes below 1000 with (roofseq primes 1000)
19:47JamesIrySo you need two sequences. One is the sequence you're looking through and the other is the sequence that has passed the test so far.
19:47Lau_of_DKyea, the idea was just to take the nth, start with 1 and move up, once I exceed the roof I'll return the _rest_
19:48Lau_of_DKthats why Im conj'ing on the next value
19:48JamesIryTaking the nth can be an expensive operation depending on the type of sequence
19:48Lau_of_DKHow would you go about it?
19:49Lau_of_DKI tried (filter (fn [x] (> x 5)) fibs)... but that of course doesn't figure out that it should break on the first false value, so it keeps going
19:50JamesIryPlus, it's the opposite meaning
19:51Lau_of_DKdetails, details
19:51JamesIry:)
19:51Lau_of_DKBut can you help me workout the rootseq?
19:51JamesIryJust a sec
19:51JamesIryI wanted to see if clojure had an equivalent to Haskell's takeWhile
19:52JamesIryThe gist is that you have a function that takes a sequence and a predicate (a function that returns true or false when given an element). Internally it calls a second recursive function with the sequence as the first argument and an empty sequence as the second.
19:53Lau_of_DKok :)
19:55JamesIryThe recursive function examines the first element of the first sequence which I'll call candidate. If candidate passes the predicate the function recurses on itself with the rest of the first sequence as the first argument plus candidate conjed onto the second argument as the new second argument. If candidate does not pass then the result is the second argument reversed.
19:55JamesIryIn other words it shrinks one sequence while building up the other.
19:55Lau_of_DKk
19:56JamesIryThe base case is when it finds the first element that doesn't match the predicate
19:57JamesIryOh yeah, second base case when the first argument is an empty sequence :-)
19:58Lau_of_DKok
20:01Lau_of_DKoh wait... isnt (rest) lazy ?
20:01Lau_of_DK(first fibs)
20:01Lau_of_DK0
20:01Lau_of_DK(rest fibs)
20:01Lau_of_DKjava heap space exhausted...
20:01Lau_of_DK??
20:04JamesIryThat's a bit surprising
20:04Lau_of_DKyea - I know my stream lib in SBCL would have handled it differently
20:04Lau_of_DK(which is not a sign of quality :))
20:04JamesIryI just thought of a better way to write takeWhile in clojure :-)
20:04Lau_of_DKGo go go! :)
20:06JamesIryHmmm...my clojure is pretty weak
20:06Lau_of_DKThat makes 2 of us
20:07Lau_of_DKWhy does this simple loop crash?
20:07Lau_of_DK(def roofseq
20:07Lau_of_DK (fn [s roof]
20:07Lau_of_DK (loop [offset 1]
20:07Lau_of_DK (if (> (first (take offset s)) roof)
20:07Lau_of_DK (take offset s)
20:07Lau_of_DK (recur (inc offset))))))
20:09JamesIryI'm struggling to write the clojure (I'm learning I'm learning) but the possibly better takeWhile than what I wrote before is to mimic Haskell's exactly...
20:09JamesIrytakeWhile takes a predicate and a sequence. Base cases are empty sequence or candidate that doesn't match predicate. For the base case it returns an empty sequence.
20:10JamesIryFor the non base case it returns a lazy stream consisting of the candidate as the head and (takeWhile p (rest s)) as the tail
20:10JamesIryp is the predicate argument, s is the stream argument
20:12Lau_of_DKsounds good
20:14JamesIryLOL, or just use take-while. http://clojure.sourceforge.net/reference/sequences.html
20:14Lau_of_DKARGH
20:14Lau_of_DK:D
20:15Lau_of_DKand I just got mine working
20:15Lau_of_DKJust replace (first ) with (last ) ... man
20:15Lau_of_DKWhy do I do this to myself, coding after midnight :(
20:18JamesIryAnd here's the impl, which is what I was describing.... (defn take-while [pred coll] (when (and (seq coll) (pred (first coll))) (lazy-cons (first coll) (take-while pred (rest coll)))))
20:19Lau_of_DKsounds easy enough - wonder why I couldnt do that
20:19JamesIryThe only thing I would be tempted to do differently is add a let to bind (first col) once
20:22Lau_of_DKwhy would you do that?
20:23JamesIryBecause it's used twice in the function
20:23JamesIryPlus adding a let helps self document
20:23Lau_of_DKah ok
20:23Lau_of_DKgood practice probably
20:37Lau_of_DKGreat, I've solved the first 3 Eulers in Clojure and learned a few tips and tricks doing it. Thanks for all your help JamesIry and Chouser
20:37JamesIryNP
20:48rhickeyChouser: for take-ns, see partition
20:54JamesIryrhickey: have you seen this - http://openjdk.java.net/projects/mlvm/jvmlangsummit/
23:35Chouserrhickey: partition is a much better name, thanks.