Clojure and me has moved.

Tuesday, March 3, 2009

Simple variations on state machines in Clojure

This post has moved, go to its new location
Given a transition function that takes the current state and an input value as arguments then (reduce transition-fn initial-state input) returns the final state.

If you are interested in intermediate states, you can use clojure.contrib.seq-utils/reductions instead of reduce.

If you want an asynchronous state machine, you can use an agent:
(def agt (agent initial-state))
;then each time you get an input-value:
(send agt transition-fn input-value)
If you add a watch to the agent, you can react to state transitions.

No comments: