diff options
author | Mark H Weaver <mhw@netris.org> | 2015-06-10 17:50:27 -0400 |
---|---|---|
committer | Mark H Weaver <mhw@netris.org> | 2015-06-10 17:50:27 -0400 |
commit | 14928016556300a6763334d4279c3d117902caaf (patch) | |
tree | d0dc262b14164b82f97dd6e896ca9e93a1fabeea /emacs/guix-utils.el | |
parent | 1511e0235525358abb52cf62abeb9457605b5093 (diff) | |
parent | 57cd353d87d6e9e6e882327be70b4d7b5ce863ba (diff) | |
download | patches-14928016556300a6763334d4279c3d117902caaf.tar patches-14928016556300a6763334d4279c3d117902caaf.tar.gz |
Merge branch 'master' into core-updates
Diffstat (limited to 'emacs/guix-utils.el')
-rw-r--r-- | emacs/guix-utils.el | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/emacs/guix-utils.el b/emacs/guix-utils.el index 823c646610..dc0c58a114 100644 --- a/emacs/guix-utils.el +++ b/emacs/guix-utils.el @@ -1,6 +1,6 @@ -;;; guix-utils.el --- General utility functions +;;; guix-utils.el --- General utility functions -*- lexical-binding: t -*- -;; Copyright © 2014 Alex Kost <alezost@gmail.com> +;; Copyright © 2014, 2015 Alex Kost <alezost@gmail.com> ;; This file is part of GNU Guix. @@ -170,6 +170,35 @@ accessed with KEYS." "Same as `diff', but use `guix-diff-switches' as default." (diff old new (or switches guix-diff-switches) no-async)) + +;;; Memoizing + +(defun guix-memoize (function) + "Return a memoized version of FUNCTION." + (let ((cache (make-hash-table :test 'equal))) + (lambda (&rest args) + (let ((result (gethash args cache 'not-found))) + (if (eq result 'not-found) + (let ((result (apply function args))) + (puthash args result cache) + result) + result))))) + +(defmacro guix-memoized-defun (name arglist docstring &rest body) + "Define a memoized function NAME. +See `defun' for the meaning of arguments." + (declare (doc-string 3) (indent 2)) + `(defalias ',name + (guix-memoize (lambda ,arglist ,@body)) + ;; Add '(name args ...)' string with real arglist to the docstring, + ;; because *Help* will display '(name &rest ARGS)' for a defined + ;; function (since `guix-memoize' returns a lambda with '(&rest + ;; args)'). + ,(format "(%S %s)\n\n%s" + name + (mapconcat #'symbol-name arglist " ") + docstring))) + (provide 'guix-utils) ;;; guix-utils.el ends here |