Some swear by monads, others (like myself) tend to swear at them. I would
suggest breaking the code apart a bit more. From the looks of it, you're
writing something like a game. The first 4 clauses could be redone as
something like this:

(def dirs
  {:left [-10 0]
   :up [0 -10]
   :right [0 10]
   :down [10 0]})

(def key-maps
   {:t-key :left})

(reduce
  (fn [acc key]
      (if (pressed? key)
          (update-in acc [:position] v+ (-> key key-maps dirs))
           acc))
   state
   keys)


Using the new reducers lib, you should be able get tolerable performance
from code like this. If you want other states (pressed, held, etc.) think
about using a multi method:

(defmulti key-dispatch (fn [state key]
                                     [(to-char key) (pressed? key) (held?
key) (released? key)]))

(defmethod key-dispatch [\t true false false]
    [state key]
    (do-stuff))

Timothy


On Mon, Feb 11, 2013 at 1:12 PM, Ben Wolfson <wolf...@gmail.com> wrote:

> 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.
>
>
>


-- 
“One of the main causes of the fall of the Roman Empire was that–lacking
zero–they had no way to indicate successful termination of their C
programs.”
(Robert Firth)

-- 
-- 
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.


Reply via email to