core/autoload/memoize: another refactor (restore doom-memoize)
This commit is contained in:
parent
d7b69b3675
commit
33120cd64e
1 changed files with 12 additions and 10 deletions
|
@ -6,23 +6,25 @@
|
||||||
"A lookup table containing memoized functions. The keys are argument lists,
|
"A lookup table containing memoized functions. The keys are argument lists,
|
||||||
and the value is the function's return value.")
|
and the value is the function's return value.")
|
||||||
|
|
||||||
(defsubst doom--memoize-wrap (name)
|
;;;###autoload
|
||||||
"Return the memoized version of FUNC."
|
(defun doom-memoize (name)
|
||||||
|
"Memoizes an existing function. NAME is a symbol."
|
||||||
(let ((func (symbol-function name)))
|
(let ((func (symbol-function name)))
|
||||||
`(lambda (&rest args)
|
(put name 'function-documentation
|
||||||
,(documentation name)
|
(concat (documentation func) " (memoized)"))
|
||||||
(let ((key (cons ',name args)))
|
(fset name
|
||||||
(or (gethash key doom-memoized-table)
|
`(lambda (&rest args)
|
||||||
(puthash key (apply ',func args) doom-memoized-table))))))
|
(let ((key (cons ',name args)))
|
||||||
|
(or (gethash key doom-memoized-table)
|
||||||
|
(puthash key (apply ',func args)
|
||||||
|
doom-memoized-table)))))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defmacro def-memoized! (name arglist &rest body)
|
(defmacro def-memoized! (name arglist &rest body)
|
||||||
"Create a memoize'd function. NAME, ARGLIST, DOCSTRING and BODY
|
"Create a memoize'd function. NAME, ARGLIST, DOCSTRING and BODY
|
||||||
have the same meaning as in `defun'."
|
have the same meaning as in `defun'."
|
||||||
(declare (indent defun) (doc-string 3))
|
(declare (indent defun) (doc-string 3))
|
||||||
(when (stringp (car body))
|
|
||||||
(setcar body (concat (car body) " (memoized)")))
|
|
||||||
`(progn
|
`(progn
|
||||||
(defun ,name ,arglist ,@body)
|
(defun ,name ,arglist ,@body)
|
||||||
(fset ',name (doom--memoize-wrap ',name))))
|
(doom-memoize ',name)))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue