Not really. The difference is between using the Repl and Script
classes. Repl, after running your code, hangs around indefinitely
with the input and output stream open, waiting for you to type more
expressions. Script, after running your code, flushes and closes the
output stream.
It really comes down to your exit case -- when do you want your
program to quit? Right now it never ends. If you make that explicit
before the end of your .clj file, you'll probably see what you were
expecting. For example, you could add this to the end:
(doseq x (repeatedly #(Thread/sleep 100)))
In a "real" program you'd probably want a more clever exit case --
after a certain number of items have been consumed, when a certain
event occurs, or whatever.
By the way, if this isn't just for practice with agents, some of the
functionality you've produced is already available with seque:
(def producer
(seque 1 (repeatedly #(let [i (rand-int 99)]
(println "producer:" i)
i))))
(doseq o (take 10 producer)
(println "consumer:" o)
(Thread/sleep 1000))
--Chouser
I don't know of any such test, but I don't think it's generally
needed. If this is your whole app, there's no real benefit in having
both the producer and consumer running in the non-main thread. For
example if you put the consumer in the main thread by just evaluating
it directly instead of using send-off, then that allow it work equally
well in the Repl and the Script.
Sleeping forever at the end of the script is not really a great
solution. I probably shouldn't have suggested it. :-)
> Is 'seque' a relatively new addition to Clojure? I could find the
> docs on clojure.org ... or may I just didn't look hard enough.
Added July 3rd. The online docs do tend to lag a bit.
--Chouser
> So for example is it possible to do something like this at
> the end of file:
>
> (when (running-as-script)
> (doseq x (repeatedly #(Thread/sleep 100))))
I do it with this idiom:
(when *command-line-args*
...)
Check out Script.java for more info.
-Drew