clojure - Passing state between macros -
what best way create set of macros share information each other @ compile time in clojure?
i'm looking way of having macros know previous macros have done , act accordingly, e.g. how implement macros can used following:
(macro-block-with-data ["a" "b" "c"] (process-next-data-item) ; macro expands using "a" (process-next-data-item) ; macro expands using "b" (process-next-data-item)) ; macro expands using "c"
clarifications
- this needs happen @ compile time macros, i.e. not regular functions @ runtime
is lexically scoped? if so, can following, uses stateful iterators (kind of ugly, though).
(defmacro process-next-data-item [] `(println "step" (.next ~'__state))) (defmacro macro-block-with-data [dat & body] `(do (let [~'__state (.iterator ~dat)] ~@body)))
as example:
(defn test [] (macro-block-with-data ["a" "b" "c"] (println "start") (process-next-data-item) ; macro expands using "a" (println "middle") (process-next-data-item) ; macro expands using "b" (println "almost done") (macro-block-with-data [ "nesteda" "nestedb" ] (println "starting nested") (process-next-data-item) (process-next-data-item) (println "done nested")) (process-next-data-item) (println "done")))
... results in ...
user> (test) start step middle step b done starting nested step nesteda step nestedb done nested step c done nil
Comments
Post a Comment