Created
December 29, 2012 01:33
-
-
Save swannodette/4403838 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
(defmacro tabled | |
"Macro for defining a tabled goal. Prefer ^:tabled with the | |
defne/a/u forms over using this directly." | |
[args & grest] | |
(let [uuid (symbol (str "tabled-" (UUID/randomUUID)))] | |
`(fn ~uuid [~@args] | |
(let [argv# ~args] | |
(fn [a#] | |
(let [key# (-reify a# argv#) | |
tables# (:ts a#) | |
tables# (if-not (contains? @tables# ~uuid) | |
(swap! tables# | |
(fn [v#] | |
(if (contains? v# ~uuid) | |
v# | |
(assoc v# ~uuid (atom {}))))) | |
@tables#) | |
table# (get tables# ~uuid) | |
cache# (get @table# key#)] | |
(if (nil? cache#) | |
(let [cache# (atom #{})] | |
(swap! table# assoc key# cache#) | |
((fresh [] | |
~@grest | |
(master argv# cache#)) a#)) | |
(reuse a# argv# cache# nil nil)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment