yes, you could do this with a state monad, though how the results will end up looking will depend on the particular monad implementation you go for.
On Mon, Feb 11, 2013 at 12:10 PM, JvJ <kfjwhee...@gmail.com> wrote: > I'm writing a simple game engine in Clojure, and each game object supplies > its own unique update function, which takes the original object (a map of > properties) and returns an updated version. However, writing the updates is > somewhat cumbersome because each line of code has to return either the > original or updated object. I'd like to see if I can clean up this code, > possibly by using monads (which I don't understand very well). Does anyone > have any advice? Thanks (Code examples below) > > The pseudocode for what i want to do looks something like this: > > if left key is held > g.position += [-10 0] > if right key is held > g.position += [10 0] > if up key is held > g.position += [0 -10] > if down key is held > g.position += [0 10] > if q is pressed > fire event {:type :dialogue, :text "Hello"} > if space is pressed > g.switchstate(:s2) > > > But the code I ended up writing is this mess: > > > (fn [g] > (-> g > (#(if (@*keys-held* :left) > (update-in % [:position] v+ [-10 0]) > %)) > (#(if (@*keys-held* :right) > (update-in % [:position] v+ [10 0]) %)) > (#(if (@*keys-held* :up) > (update-in % [:position] v+ [0 -10]) %)) > (#(if (@*keys-held* :down) > (update-in % [:position] v+ [0 10]) %)) > (#(if (@*keys-pressed* \q) > (do (fire-event {:type :dialogue > :text "Hello!"}) > %) > %)) > (#(if (@*keys-pressed* :space) > (do (comment (println "spaced!")) > (switch-state % :s2)) %)))) > > -- > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > -- Ben Wolfson "Human kind has used its intelligence to vary the flavour of drinks, which may be sweet, aromatic, fermented or spirit-based. ... Family and social life also offer numerous other occasions to consume drinks for pleasure." [Larousse, "Drink" entry] -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.