Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created December 29, 2012 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/4403838 to your computer and use it in GitHub Desktop.
Save swannodette/4403838 to your computer and use it in GitHub Desktop.
(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