-
-
Save AeroNotix/2a7cb69f29a4d109447f to your computer and use it in GitHub Desktop.
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
(ns crap.actors) | |
(defn parse-fn [[_ arg-spec & body]] | |
`(fn ~arg-spec ~@body)) | |
(defn parse-actor-spec [spec] | |
(loop [m {:validator (fn [_] true)} s spec] | |
(if (seq s) | |
(case (first (first s)) | |
on-exception | |
(recur (assoc m :on-exception | |
(parse-fn (first s))) | |
(rest s)) | |
validator | |
(recur (assoc m :validator | |
(parse-fn (first s))) | |
(rest s)) | |
initial-state | |
(recur (assoc m :initial-state | |
(second (first s))) | |
(rest s)) | |
(throw (Exception. "Unknown actor specification"))) | |
m))) | |
(defmacro actor | |
"Defines an actor in a more declarative way than typically allowed | |
when using simple agents. | |
(defn foo | |
(actor | |
(initial-state | |
(do | |
(println \"foo\") | |
:something)) | |
(validator [a] | |
(println a) | |
true) | |
(on-exception [a e] | |
(println \"foooo\")))" | |
[& spec] | |
(let [parsed-spec (parse-actor-spec spec)] | |
`(do | |
(let [actor# (agent ~(:initial-state parsed-spec))] | |
(set-error-handler! actor# ~(:on-exception parsed-spec)) | |
(set-validator! actor# ~(:validator parsed-spec)) | |
actor#)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment