Last active
November 27, 2018 06:17
-
-
Save RutledgePaulV/c088a63634e987c8c984bd218fc50f4c to your computer and use it in GitHub Desktop.
A macro for memoization of a body of code based on the values of all referenced vars (implicit memoization key instead of explicit arguments). Probably a terrible idea.
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 cached [& body] | |
(let [dependencies (transient #{}) | |
resolved (walk/postwalk | |
(fn [form] | |
(if (symbol? form) | |
(if-some [resolved (resolve &env form)] | |
(when (var? resolved) | |
(conj! dependencies resolved) | |
(.toSymbol ^Var resolved)) | |
form) | |
form)) | |
`(do ~@body))] | |
`(let [vars# ~(persistent! dependencies) | |
inner# (memoize (fn [_#] (eval ~resolved)))] | |
(fn [] (inner# (mapv var-get vars#)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment