cw, I have been converting the examples from "Practical Common Lisp" into Clojure. A big part of this is switching from imperative to functional mindset. You might want to look at Chapters 3, 16, and 17.
http://blog.thinkrelevance.com/2008/9/16/pcl-clojure Cheers, Stuart > > Hi all. > > First of all, thanks for the joyful language, Clojure. > > I am having struggle in understanding functional programming, > which Clojure dictates. > Though I'm not a big fan of object oriented programming, > I totally agree with the model of objects - objects and methods, > entities and functions in other words, or > states and means to change the states. > > So, I understand as follows: > - OOP: keeping objects, which has states and methods. > methods are encapuslated to the corresponding object. > - FP: keeping objects(or structs or variables, whatever it is called) > and functions on the objects. > functions does not encapsulated to specific objects, for some > reasons. > > Other characteristics such as function as first-order data structure > and > enforcing functional way (discourated side effects and immutable > variables) > can be important, but not in modeling the world. > > So, main question is these. > (1) I might know that there are some benefits not to bind functions to > specific > objects (class). But I think that functions can be tied to > specific objects (class) > and it's more convienient in many situations. By tieing, I do mean > logical concept, > not physical syntax. > So, I guess that any small piece of programs in FP consist of > entity definitons and > corresponding functions which operates on the entity. > Am I right? Is it correct or idiomatic ways in FP? > > (2) Second question is specific to Clojure. > Let's think of an example. We need to make a Web server. > Session is typical modeling entites in this problem. > Session begins(instantiates) upon the arrival of a client request, > receives HTTP request, parses the request, search the storage for > given > requests, generates the response, and sends the response. > Since Session has to keep states such as client information, > received requests > and corresponding data, I'll create Session as an object in > conventional OOP and > call the method like this on a dedicated thread. > do_session() { > receive_and_parse(); > lookup(); > generate(); > send(); > } > I know it's easy way to do like this, rather than do > asynchronosly. > > In Clojure, what's the best way? > > My first guess is: > (def session ..) > (defn do_session [..] (receive_and_parse) (lookup) (generate) > (send)) > > Since the default stack size of a Java thread (over 512KB) is much > bigger than > that of native threads (8KB NPTL), above approch may suffer from > low number of > maximum concurrent threads. So my second guess is using _agent_ in > Clojure. > As far as I understands, agent in Clojure is a state which can > send and > receive messages. Then, by using agent in Clojure, does one must > do > a kind of state-machine programming? > With above example, > session agent can > have :initial, :after_receive, :after_lookup, :after_generate, > and :end. > I may use an asynchornous IO facility (I never used Java, but I > guess there is one) > to dispatch IO completion notification and send messages to the > agents. > But this is too complex! Everybody favors thread programming > because it's easy. > In each state of session agent, there can be sub-state like: > :in_receving, :in_sending, :in_looking, ... for nature of IO > operations. > So complex. Asynchornous IO with state management is inherent > comple, in my opinion. > > So my third guess is "There must be a right way to do this." > Maybe this question is related to the first one. > > But for the lack of experience in FP and clojure, I don't know how > to. > > Thank you. > > - cw > > > --~--~---------~--~----~------------~-------~--~----~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---