2008-06-22
| 01: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 | meredydd | 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 | meredydd | 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 | spacebat_ | say I've got this function |
| 18:06 | spacebat_ | (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 | spacebat_ | 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 | spacebat_ | 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 | spacebat_ | my various attempts have thrown different exceptions, commonly illegal casts between Integer and clojure Var |
| 18:19 | spacebat_ | which seems strange :) |
| 18:23 | dudleyf | pjb3: {:foo 1} gets converted to a seq, so you end up with (:foo [:foo 1]) |
| 18:33 | dudleyf | spacebat_: Vars should be immutable |
| 18:34 | dudleyf | If you want mutable state, you need to use a Ref |
| 18:34 | dudleyf | I think |
| 18:56 | pjb3 | dudleyf: so (apply :foo {:foo 1}) => nil, but (:foo {:foo 1}) => 1 |
| 18:57 | pjb3 | That is expected? |
| 19:10 | shizzy0 | Hello guys. I wrote up a unit-test library for Clojure I wanted to announce. |
| 19:10 | shizzy0 | http://gnufoo.org/clojure/unit-test/README.html |
| 19:11 | jgrant | hey everyone |
| 19:11 | jgrant | clojure newbie here |
| 19:12 | jgrant | question about dealing with exceptions thrown by the jvm |
| 19:12 | jgrant | why does this catch the exception : |
| 19:12 | shizzy0 | hello |
| 19:12 | jgrant | (try (. System hello) (catch Exception ex ex)) |
| 19:12 | jgrant | java.lang.NoSuchFieldException: hello |
| 19:13 | jgrant | i mean why does it NOT catch the exception (oops) |
| 19:13 | shizzy0 | Huh. |
| 19:13 | shizzy0 | Let me try it. |
| 19:14 | shizzy0 | 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 | jgrant | crappy test example on my part |
| 19:23 | jgrant | here's a better question : |
| 19:23 | shizzy0 | eh, it's nonobvious. |
| 19:23 | jgrant | (import '(java.io '*)) |
| 19:24 | jgrant | doesn't import all classes in that java package ? |
| 19:25 | shizzy0 | good question. |
| 19:25 | shizzy0 | I'm not sure. |
| 19:27 | jgrant | anyone else have an idea ? |
| 19:32 | jgrant | (defn import |
| 19:32 | jgrant | "import-list => (package-symbol class-name-symbols*) |
| 19:32 | jgrant | |
| 19:32 | jgrant | For each name in class-name-symbols, adds a mapping from name to the |
| 19:32 | jgrant | class named by package.name to the current namespace." |
| 19:32 | jgrant | [& import-lists] |
| 19:32 | jgrant | (when import-lists |
| 19:32 | jgrant | (let [#^clojure.lang.Namespace ns *ns* |
| 19:32 | jgrant | pkg (ffirst import-lists) |
| 19:32 | jgrant | classes (rfirst import-lists)] |
| 19:32 | jgrant | (doseq c classes |
| 19:32 | jgrant | (. ns (importClass c (. Class (forName (str pkg "." c)))))) ) |
| 19:32 | jgrant | (apply import (rest import-lists)))) |
| 19:32 | jgrant | looks like it's not supported yet |
| 19:32 | jgrant | but easy enough to add |
| 20:54 | jgrant | Another question... |
| 20:54 | jgrant | ... how do you perform a while loop in clojure ? |
| 21:00 | shizzy0 | (loop [x initial-value] |
| 21:01 | shizzy0 | (recur (next-value x))) |
| 21:01 | shizzy0 | Loop is probably what you want to look at. |
| 21:02 | shizzy0 | but I'd actually like some other macro to do a while. |
| 21:02 | shizzy0 | it's doable, it just seems a little off. |
| 21:03 | shizzy0 | 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 | rhickey | 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 | dudleyf | 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 | jgrant | (.. 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 | pjb3 | This works: (reduce (fn [a b] (+ (a 1) (b 1))) '([:a 1] [:b 2])) |
| 21:59 | pjb3 | This: (reduce (fn [a b] (+ (a 1) (b 1))) {:a 1 :b 2}) |
| 21:59 | pjb3 | gives me java.lang.ClassCastException: clojure.lang.PersistentHashMap$LeafNode |
| 21:59 | pjb3 | how do you map/reduce items of a map? |
| 22:00 | jgrant | (.. Thread (currentThread) (isAlive)) |
| 22:00 | jgrant | that works |
| 22:00 | jgrant | so why not... |
| 22:00 | jgrant | (.. 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 | pjb3 | 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 | jgrant | Thread.sleep(100) |
| 22:03 | jgrant | i know |
| 22:03 | jgrant | so whats the equiv in clojure ? |
| 22:03 | pjb3 | so just (. Thread (sleep 100)) |
| 22:04 | jgrant | thank you pbj3 |
| 22:04 | jgrant | duh |
| 22:04 | pjb3 | also, (.sleep Thread 100) |
| 22:04 | pjb3 | 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 | jgrant | 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 | jgrant | rich : is there something like while in clojure ? |
| 22:08 | rhickey | loop/recur |
| 22:09 | rhickey | you could use that to write a dowhile |
| 22:09 | rhickey | while almost always being for side-effects |
| 22:09 | jgrant | i see |
| 22:13 | jgrant | rich : btw - having loads of fun with clojure so far, thx ! |
| 22:14 | rhickey | great! you're welcome |