Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created December 11, 2014 21:03
Show Gist options
  • Save selfsame/ae3aff95abe3957b2530 to your computer and use it in GitHub Desktop.
Save selfsame/ae3aff95abe3957b2530 to your computer and use it in GitHub Desktop.
async loop macro
(defmacro defstate [name state & inputs]
(let [[sym init] state
pairs (mapv vec (partition 2 inputs))
in (gensym)
dispatch (fn [i [f r]]
(~'or (~'and (~'fn? f)
(f i))
(~'= f i)
r))]
`(do
(~'def ~name (~'chan (async/sliding-buffer 1)))
(~'go
(~'loop [~sym ~init]
(~'prn ~sym)
(~'let [~in (~'<! ~name)]
(~'recur
(~'last
(~'first
(~'filter
(~'fn [[~'i ~'r]]
(~'or
(~'and (~'fn? ~'i)
(~'i ~in))
(~'= ~'i ~in)))
~pairs)))
)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment