#clojure log - Jun 22 2008

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

1:13 arbscht: hrm, is svn rv 917 broken?

16:18 meredydd: Hey - apologies for the RTFM failure, but I'm using my phone and HTTP is terrible

16:18 Is there a clojure-native thread-pool system?

16:18 rhickey: agents use thread pools behind the scenes

16:18 meredydd: uh-huh

16:19 I have a set of tasks which I'm happy to let lots of threads deal with in parallel

18:05 spacebat_: hi, I have a newb question about clojure

18:06 say I've got this function

18:06 (defn fib [n] (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))

18:06 pjb3: Why does (apply :foo {:foo 1}) return nil?

18:07 spacebat_: I'd like to make it a closure over a variable, x, that counts the number of times (fib) is called

18:07 pjb3: spacebat_: Use let and recur?

18:08 spacebat_: thanks I'll look up recur

18:08 I tried let on its own but I think those values are immutable

18:09 pjb3: err, I think you want to use loop and recur

18:15 spacebat_: I see the factorial example using loop and recur, but it seems when the function exits those variables are unbound

18:16 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:18 my various attempts have thrown different exceptions, commonly illegal casts between Integer and clojure Var

18:19 which seems strange :)

18:23 dudleyf: pjb3: {:foo 1} gets converted to a seq, so you end up with (:foo [:foo 1])

18:33 spacebat_: Vars should be immutable

18:34 If you want mutable state, you need to use a Ref

18:34 I think

18:56 pjb3: dudleyf: so (apply :foo {:foo 1}) => nil, but (:foo {:foo 1}) => 1

18:57 That is expected?

19:10 shizzy0: Hello guys. I wrote up a unit-test library for Clojure I wanted to announce.

19:10 http://gnufoo.org/clojure/unit-test/README.html

19:11 jgrant: hey everyone

19:11 clojure newbie here

19:12 question about dealing with exceptions thrown by the jvm

19:12 why does this catch the exception :

19:12 shizzy0: hello

19:12 jgrant: (try (. System hello) (catch Exception ex ex))

19:12 java.lang.NoSuchFieldException: hello

19:13 i mean why does it NOT catch the exception (oops)

19:13 shizzy0: Huh.

19:13 Let me try it.

19:14 huh, I think that it's actually causing an exception at compile time.

19:14 jgrant: yea your right

19:15 shizzy0: that's why it's not being caught.

19:15 jgrant: thanks

19:15 crappy test example on my part

19:23 here's a better question :

19:23 shizzy0: eh, it's nonobvious.

19:23 jgrant: (import '(java.io '*))

19:24 doesn't import all classes in that java package ?

19:25 shizzy0: good question.

19:25 I'm not sure.

19:27 jgrant: anyone else have an idea ?

19:32 (defn import

19:32 "import-list => (package-symbol class-name-symbols*)


19:32 For each name in class-name-symbols, adds a mapping from name to the

19:32 class named by package.name to the current namespace."

19:32 [& import-lists]

19:32 (when import-lists

19:32 (let [#^clojure.lang.Namespace ns *ns*

19:32 pkg (ffirst import-lists)

19:32 classes (rfirst import-lists)]

19:32 (doseq c classes

19:32 (. ns (importClass c (. Class (forName (str pkg "." c)))))) )

19:32 (apply import (rest import-lists))))

19:32 looks like it's not supported yet

19:32 but easy enough to add

20:54 Another question...

20:54 ... how do you perform a while loop in clojure ?

21:00 shizzy0: (loop [x initial-value]

21:01 * shizzy0 (if (something? x)

21:01 shizzy0: (recur (next-value x)))

21:01 Loop is probably what you want to look at.

21:02 but I'd actually like some other macro to do a while.

21:02 it's doable, it just seems a little off.

21:03 the (for) macro has a :while condition, you can use.

21:22 dudleyf: pjb3: (apply foo bar) is not necessarily supposed to be the same as (foo bar)

21:23 rhickey: right, never the same

21:24 dudleyf: never?

21:24 rhickey: apply is fundamentally apply supplying a list of args as a single arg

21:24 dudleyf: ah, it only works on seqs, not single values

21:24 rhickey: the last arg to apply must be a collection, gets unrolled into separate args

21:25 the collection can be preceded by single args (apply + 1 2 [3 4 5])

21:25 dudleyf: I was assuming that apply would pass a singular argument as a single arg

21:27 I've always called that operation "splatting", for some reason

21:28 rhickey: if there's no collection, there's no need for apply

21:52 pjb3: Ah, makes sense, so (foo {:foo 1}) == (apply :foo '({:foo 1}))

21:54 dudleyf: Yep

21:54 rhickey: right, or (apply :foo [{:foo 1}]) - more idiomatic than quoted lists

21:57 jgrant: Why does this not work ?

21:57 (.. Thread (currentThread) (sleep 100))

21:58 pjb3: Ok, so now I'm trying to do something to each k/v pair of a map

21:58 This works: (reduce (fn [a b] (+ (a 1) (b 1))) '([:a 1] [:b 2]))

21:59 This: (reduce (fn [a b] (+ (a 1) (b 1))) {:a 1 :b 2})

21:59 gives me java.lang.ClassCastException: clojure.lang.PersistentHashMap$LeafNode

21:59 how do you map/reduce items of a map?

22:00 jgrant: (.. Thread (currentThread) (isAlive))

22:00 that works

22:00 so why not...

22:00 (.. Thread (currentThread) (sleep 100))

22:00 pjb3: ok, solved that one: (reduce (fn [a b] (+ (val a) (val b))) {:a 1 :b 2})

22:02 jgrant: sleep is a static method

22:03 jgrant: right so how do you call it ?

22:03 pjb3: what you are doing would be Thread.currentThread().sleep(100) in Java, which wouldn't work

22:03 jgrant: ok

22:03 Thread.sleep(100)

22:03 i know

22:03 so whats the equiv in clojure ?

22:03 pjb3: so just (. Thread (sleep 100))

22:04 jgrant: thank you pbj3

22:04 duh

22:04 pjb3: also, (.sleep Thread 100)

22:04 which I prefer

22:04 jgrant: yea very slick

22:05 dudleyf: pjb: (reduce #(+ (val %1) (val %2)) {:a 1 :b 2})

22:05 rhickey: also: (Thread/sleep 100)

22:05 dudleyf: saves a couple of parens

22:05 pjb3: dudleyf: nice, I like that

22:05 jgrant: ah !

22:06 thx rich

22:07 rhickey: also: (.. Thread currentThread isAlive)

22:07 jgrant: yea got that one i was tripping over the static call for Thread.sleep(1000)

22:08 rich : is there something like while in clojure ?

22:08 rhickey: loop/recur

22:09 you could use that to write a dowhile

22:09 while almost always being for side-effects

22:09 jgrant: i see

22:13 rich : btw - having loads of fun with clojure so far, thx !

22:14 rhickey: great! you're welcome

Logging service provided by n01se.net