Created
December 11, 2014 21:03
-
-
Save selfsame/ae3aff95abe3957b2530 to your computer and use it in GitHub Desktop.
async loop macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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