On Wed, Jun 16, 2010 at 12:17 AM, Christophe Grand <christo...@cgrand.net> wrote: > 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 [] >
Thank you - I was unaware that a function sent to an agent had to accept at least one arg. >> (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))) > Very true - in this example, I totally agree. The example I put together is a simplified version of the program I'm working on which uses some file I/O. I'm wanting to tail a file and process its contents over time so I had a loop that would read the file until it started reading 'nil', then sleep, then try again, creating a loop within a loop (and polling the file for new contents). In trying to keep the example program faithful to the other, I ended up with some nonsensical clojure ; ) > >> ;;; >> (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))) > Here my near total ignorance of futures comes into play. Thank you very much for pointing this out as a better way. I've been so eager to write a first program in clojure that I haven't gotten through all the 1.1 (and 1.0) language features yet. :D > ;;; > (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) > I appreciate yours and everyone's valuable time! -- 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