Hi Ryan,

On Tue, Jun 15, 2010 at 6:01 PM, Ryan Waters <ryan.or...@gmail.com> wrote:
> I'm working with the code at the following gist and also pasted below:
>
> http://gist.github.com/421550
>
> I'd like to have execution of a separate thread (agent) continue
> running until it sees the atom 'running' change to false.
>
> Unfortunately, the program doesn't return from the send-off but
> to my understanding it should.  Why won't it return?  I'm using
> clojure 1.1.

It does return from send-off -- in your gist's version at last thanks
to Shawn but there are still other errors.

> (ns nmanage)
>
> (def running (atom true))
>
> (defn process
>  []

A fn sent as an action to an agent must accept at least one arg: the
agent's current value (if additional args are passed to args, the
action fn must accept them too). So the above line should be [_] (_ to
signify that you don't care about the value) instead of []

>  (when @running
>    (prn "hi")
>    (Thread/sleep 1000))
>  (recur))

Here your recur is misplaced, you want to recur when @running is true
so the (recur) should be at the end of the when form and it should
have an extra argument since we changed process to take one arg.
However this when/recur pattern is what the while macro expands to.

(defn process [_]
 (while @running
   (prn "hi")
   (Thread/sleep 1000)))


> ;;;
> (send-off (agent nil) (process))

Here it should be (send-off (agent nil) process) but Shawn already
pointed this out.

I think that using an agent and not caring about its value is kind of
an abuse, better use a future.

(def running (atom true))

(future
  (while @running
    (prn "hi")
    (Thread/sleep 1000)))

;;;
(do
  (prn "this prints now - fixed thanks to Shawn Hoover")
  (Thread/sleep 2000)
  (reset! running false))

hth,

Christophe

-- 
European Clojure Training Session: Brussels, 23-25/6 http://conj-labs.eu/
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to