Hi,

On Mon, May 17, 2010 at 08:49:35AM -0700, Jules wrote:

> The problem occurs in stateful objects when you receive an input and
> need to both modify your state and generate and deliver some output.
> 
> I store my internal state in an atom and use swap! to update it,
> passing in the input and a pure function which accepts the object's
> current state and input and returns the object's new state.

I believe you need a ref for this, since you have to coordinate several
states: the object state and the input state.

(defn do-stuff
  [state input]
  (dosync
    (let [[next-state output] (do-pure-stuff @state input)]
      (ref-set state next-state)
      output)))

or you combine both states in one, which might also make sense: the
current-state with the next input is a snapshot of your system from
which you can replay things.

(defn do-stuff
  [state input]
  (:output (swap! state do-pure-stuff input)))

Which is basically what you describe.

Sincerely
Meikel

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

Reply via email to