core/autoload/memoize: refactor def-memoized!
This commit is contained in:
parent
d4e25d3f0d
commit
993e1d19e5
1 changed files with 14 additions and 20 deletions
|
@ -1,32 +1,26 @@
|
||||||
;;; memoize.el
|
;;; memoize.el
|
||||||
(provide 'core-lib-memoize)
|
(provide 'doom-lib-memoize)
|
||||||
|
|
||||||
(defvar doom-memoized-table (make-hash-table :test 'equal)
|
(defvar doom-memoized-table (make-hash-table :test 'equal :size 10)
|
||||||
"TODO")
|
"A lookup table containing memoized functions. The keys are argument lists,
|
||||||
|
and the value is the function's return value.")
|
||||||
|
|
||||||
;;;###autoload
|
(defsubst doom--memoize-wrap (name)
|
||||||
(defun doom-memoize (func)
|
|
||||||
"Memoize the given function."
|
|
||||||
(cl-typecase func
|
|
||||||
(symbol (put func 'function-documentation
|
|
||||||
(concat (documentation func) " (memoized)"))
|
|
||||||
(fset func (doom--memoize-wrap (symbol-function func)))
|
|
||||||
func)
|
|
||||||
(function (doom--memoize-wrap func))))
|
|
||||||
|
|
||||||
;;;###autoload
|
|
||||||
(defun doom--memoize-wrap (func)
|
|
||||||
"Return the memoized version of FUNC."
|
"Return the memoized version of FUNC."
|
||||||
`(lambda (&rest args)
|
(let ((func (symbol-function name)))
|
||||||
(or (gethash args doom-memoized-table)
|
`(lambda (&rest args)
|
||||||
(puthash args (apply ',func args) doom-memoized-table))))
|
,(documentation name)
|
||||||
|
(let ((key (cons ',name args)))
|
||||||
|
(or (gethash key doom-memoized-table)
|
||||||
|
(puthash key (apply ',func args) doom-memoized-table))))))
|
||||||
|
|
||||||
;;;###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)
|
||||||
(doom-memoize ',name)))
|
(fset ',name (doom--memoize-wrap ',name))))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue