On Sat, Apr 21, 2012 at 11:27 AM, timc <timgcl...@gmail.com> wrote: > Hello > > I am a beginner when it comes to writing macros, so there may be an easy way > to do this. > I have a number of 'state machines' which have this sort of appearance: > > (defn startFSM [eventQ] > (let [state (atom :1) > going (atom true) > timerId (atom -1) > startTimer (fn [] ....) > stopTimer (fn [] ...)] > (inThread > (while @going > (let [ev (.take eventQ)] > (condp = @state > :1 (....) > :2 (...) > etc...)))))) > > where inThread runs its body in a thread, and the timer functions deal with > timers. So, when (startFSM aQ) is called, the thread runs and processes > events arriving on aQ. > > My question is, how to write a macro, fsmWithTimers, that could be used like > this: > > (fsmWithTimers > (condp = @state > :1 (....) > :2 (...) > etc...)) > > which would resolve into the form shown above, so that the items mentioned > in the outer let are usable (via the names specified) within the body (in > the example a condp form)?
(defmacro fsmWithTimers [& body] `(defn startFSM [eventQ#] (let [~'state (atom :1) ~'going (atom true) ~'timerId (atom -1) ~'startTimer (fn [] ....) ~'stopTimer (fn [] ...)] (inThread (while (deref ~'going) (let [~'ev (.take eventQ#)] ~@body)))))) oughta do it. -- 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