Hello, and welcome on board ! :-)
One general remark : too much things done at the top level. It's ok to define some globals such as (def listen-port 8555) but having this (def server-sock (ServerSocket. listen-port)) will raise the problem that as soon as you 'require your namespace, the server socket will be open. This may not be what you want, and certainly would cause problems to IDEs that will want to help you catch errors and re-require your namespace periodically on the background. Same problem with the direct "(run)" call at the end. Of course, if it's just a matter of launching it as a script, then this could be sufficient. (Though even scripts can have some -e "(seq-server/run)" call appended to the java -cp clojure.jar:seq-server.jar in a .sh , I guess). A style remark also : currently, writing imports (and uses, requires ...) in 'ns are preferred with angle brackets rather than parens for the args of :import, :use ...) : (:import [java.net ServerSocket]) instead of (:import (java.net ServerSocket)) Because then it looks less like an invocation, and more like data. HTH, -- Laurent 2009/11/13 Ross Thomas <halfacan...@gmail.com>: > Hello group, > > First I'd like to thank Rich for such a great new tool. The assertion > in Programming Clojure that it "feels like a general-purpose language > beamed back from the near future" really sums things up very nicely. > Homoiconicity + FP + STM + JVM = epic win. > > Anyway, thought I'd say hi, and solicit opinions on my first "non- > trivial" (that's a relative term ;)) Clojure program, a toy server > which opens a listening socket and prints a message to each connecting > client: > > ;;; > > (ns seq-server > #^{:author "Ross Thomas <halfacan...@gmail.com>"} > (:import (java.net ServerSocket))) > > (def listen-port 8555) > (def num-agents 32) > (def message "Hello, world!\r\n") > > (def server-sock (ServerSocket. listen-port)) > > (def clients (repeatedly #(.accept server-sock))) > > (def agent-pool (take num-agents (repeatedly #(agent nil)))) > > (def agents (cycle agent-pool)) > > (def work-seq (map vector clients agents)) > > (declare handle-client) > > (defn run [] > (doseq [[c a] work-seq] > (send-off a handle-client c))) > > (defn handle-client [_ client] > (.. client getOutputStream (write (.getBytes message))) > (doto client > .shutdownInput > .shutdownOutput > .close)) > > (run) > > ;;; > > I can't decide if doing it that way is a bit too weird or not, but it > was fun to write. Anyway, interested to hear comments/criticisms, how > I could make it more idiomatic, etc. :) > > -Ross > > -- > 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 -- 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