#clojure logs

2008-06-22

01:13arbschthrm, is svn rv 917 broken?
16:18meredyddHey - apologies for the RTFM failure, but I'm using my phone and HTTP is terrible
16:18meredyddIs there a clojure-native thread-pool system?
16:18rhickeyagents use thread pools behind the scenes
16:18meredydduh-huh
16:19meredyddI have a set of tasks which I'm happy to let lots of threads deal with in parallel
18:05spacebat_hi, I have a newb question about clojure
18:06spacebat_say I've got this function
18:06spacebat_(defn fib [n] (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))
18:06pjb3Why does (apply :foo {:foo 1}) return nil?
18:07spacebat_I'd like to make it a closure over a variable, x, that counts the number of times (fib) is called
18:07pjb3spacebat_: Use let and recur?
18:08spacebat_thanks I'll look up recur
18:08spacebat_I tried let on its own but I think those values are immutable
18:09pjb3err, I think you want to use loop and recur
18:15spacebat_I see the factorial example using loop and recur, but it seems when the function exits those variables are unbound
18:16spacebat_in common lisp it works just by preceding the function with (defvar x 0) and putting a (setf x (+ x 1)) in the function body
18:18spacebat_my various attempts have thrown different exceptions, commonly illegal casts between Integer and clojure Var
18:19spacebat_which seems strange :)
18:23dudleyfpjb3: {:foo 1} gets converted to a seq, so you end up with (:foo [:foo 1])
18:33dudleyfspacebat_: Vars should be immutable
18:34dudleyfIf you want mutable state, you need to use a Ref
18:34dudleyfI think
18:56pjb3dudleyf: so (apply :foo {:foo 1}) => nil, but (:foo {:foo 1}) => 1
18:57pjb3That is expected?
19:10shizzy0Hello guys. I wrote up a unit-test library for Clojure I wanted to announce.
19:10shizzy0http://gnufoo.org/clojure/unit-test/README.html
19:11jgranthey everyone
19:11jgrantclojure newbie here
19:12jgrantquestion about dealing with exceptions thrown by the jvm
19:12jgrantwhy does this catch the exception :
19:12shizzy0hello
19:12jgrant(try (. System hello) (catch Exception ex ex))
19:12jgrantjava.lang.NoSuchFieldException: hello
19:13jgranti mean why does it NOT catch the exception (oops)
19:13shizzy0Huh.
19:13shizzy0Let me try it.
19:14shizzy0huh, I think that it's actually causing an exception at compile time.
19:14jgrantyea your right
19:15shizzy0that's why it's not being caught.
19:15jgrantthanks
19:15jgrantcrappy test example on my part
19:23jgranthere's a better question :
19:23shizzy0eh, it's nonobvious.
19:23jgrant(import '(java.io '*))
19:24jgrantdoesn't import all classes in that java package ?
19:25shizzy0good question.
19:25shizzy0I'm not sure.
19:27jgrantanyone else have an idea ?
19:32jgrant(defn import
19:32jgrant "import-list => (package-symbol class-name-symbols*)
19:32jgrant
19:32jgrant For each name in class-name-symbols, adds a mapping from name to the
19:32jgrant class named by package.name to the current namespace."
19:32jgrant [& import-lists]
19:32jgrant (when import-lists
19:32jgrant (let [#^clojure.lang.Namespace ns *ns*
19:32jgrant pkg (ffirst import-lists)
19:32jgrant classes (rfirst import-lists)]
19:32jgrant (doseq c classes
19:32jgrant (. ns (importClass c (. Class (forName (str pkg "." c)))))) )
19:32jgrant (apply import (rest import-lists))))
19:32jgrantlooks like it's not supported yet
19:32jgrantbut easy enough to add
20:54jgrantAnother question...
20:54jgrant... how do you perform a while loop in clojure ?
21:00shizzy0(loop [x initial-value]
21:01shizzy0(recur (next-value x)))
21:01shizzy0Loop is probably what you want to look at.
21:02shizzy0but I'd actually like some other macro to do a while.
21:02shizzy0it's doable, it just seems a little off.
21:03shizzy0the (for) macro has a :while condition, you can use.
21:22dudleyfpjb3: (apply foo bar) is not necessarily supposed to be the same as (foo bar)
21:23rhickeyright, never the same
21:24dudleyfnever?
21:24rhickeyapply is fundamentally apply supplying a list of args as a single arg
21:24dudleyfah, it only works on seqs, not single values
21:24rhickeythe last arg to apply must be a collection, gets unrolled into separate args
21:25rhickeythe collection can be preceded by single args (apply + 1 2 [3 4 5])
21:25dudleyfI was assuming that apply would pass a singular argument as a single arg
21:27dudleyfI've always called that operation "splatting", for some reason
21:28rhickeyif there's no collection, there's no need for apply
21:52pjb3Ah, makes sense, so (foo {:foo 1}) == (apply :foo '({:foo 1}))
21:54dudleyfYep
21:54rhickeyright, or (apply :foo [{:foo 1}]) - more idiomatic than quoted lists
21:57jgrantWhy does this not work ?
21:57jgrant(.. Thread (currentThread) (sleep 100))
21:58pjb3Ok, so now I'm trying to do something to each k/v pair of a map
21:58pjb3This works: (reduce (fn [a b] (+ (a 1) (b 1))) '([:a 1] [:b 2]))
21:59pjb3This: (reduce (fn [a b] (+ (a 1) (b 1))) {:a 1 :b 2})
21:59pjb3gives me java.lang.ClassCastException: clojure.lang.PersistentHashMap$LeafNode
21:59pjb3how do you map/reduce items of a map?
22:00jgrant(.. Thread (currentThread) (isAlive))
22:00jgrantthat works
22:00jgrantso why not...
22:00jgrant(.. Thread (currentThread) (sleep 100))
22:00pjb3ok, solved that one: (reduce (fn [a b] (+ (val a) (val b))) {:a 1 :b 2})
22:02pjb3jgrant: sleep is a static method
22:03jgrantright so how do you call it ?
22:03pjb3what you are doing would be Thread.currentThread().sleep(100) in Java, which wouldn't work
22:03jgrantok
22:03jgrantThread.sleep(100)
22:03jgranti know
22:03jgrantso whats the equiv in clojure ?
22:03pjb3so just (. Thread (sleep 100))
22:04jgrantthank you pbj3
22:04jgrantduh
22:04pjb3also, (.sleep Thread 100)
22:04pjb3which I prefer
22:04jgrantyea very slick
22:05dudleyfpjb: (reduce #(+ (val %1) (val %2)) {:a 1 :b 2})
22:05rhickeyalso: (Thread/sleep 100)
22:05dudleyfsaves a couple of parens
22:05pjb3dudleyf: nice, I like that
22:05jgrantah !
22:06jgrantthx rich
22:07rhickeyalso: (.. Thread currentThread isAlive)
22:07jgrantyea got that one i was tripping over the static call for Thread.sleep(1000)
22:08jgrantrich : is there something like while in clojure ?
22:08rhickeyloop/recur
22:09rhickeyyou could use that to write a dowhile
22:09rhickeywhile almost always being for side-effects
22:09jgranti see
22:13jgrantrich : btw - having loads of fun with clojure so far, thx !
22:14rhickeygreat! you're welcome