* [PATCH 1/2] emacs: Add "memoization" code.
@ 2015-06-06 20:44 Alex Kost
2015-06-07 15:24 ` Ludovic Courtès
0 siblings, 1 reply; 2+ messages in thread
From: Alex Kost @ 2015-06-06 20:44 UTC (permalink / raw
To: guix-devel
[-- Attachment #1: Type: text/plain, Size: 41 bytes --]
Some auxiliary code for the next patch.
[-- Attachment #2: 0001-emacs-Add-memoization-code.patch --]
[-- Type: text/x-patch, Size: 2150 bytes --]
From 8f6dfdc793d39b40bf57fbc7728e815b40b7c199 Mon Sep 17 00:00:00 2001
From: Alex Kost <alezost@gmail.com>
Date: Sat, 6 Jun 2015 22:14:13 +0300
Subject: [PATCH 1/2] emacs: Add "memoization" code.
* emacs/guix-utils.el (guix-memoize): New function.
(guix-memoized-defun): New macro.
---
emacs/guix-utils.el | 33 +++++++++++++++++++++++++++++++--
1 file changed, 31 insertions(+), 2 deletions(-)
diff --git a/emacs/guix-utils.el b/emacs/guix-utils.el
index 823c646..dc0c58a 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))
+\f
+;;; 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
--
2.2.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH 1/2] emacs: Add "memoization" code.
2015-06-06 20:44 [PATCH 1/2] emacs: Add "memoization" code Alex Kost
@ 2015-06-07 15:24 ` Ludovic Courtès
0 siblings, 0 replies; 2+ messages in thread
From: Ludovic Courtès @ 2015-06-07 15:24 UTC (permalink / raw
To: Alex Kost; +Cc: guix-devel
Alex Kost <alezost@gmail.com> skribis:
> From 8f6dfdc793d39b40bf57fbc7728e815b40b7c199 Mon Sep 17 00:00:00 2001
> From: Alex Kost <alezost@gmail.com>
> Date: Sat, 6 Jun 2015 22:14:13 +0300
> Subject: [PATCH 1/2] emacs: Add "memoization" code.
>
> * emacs/guix-utils.el (guix-memoize): New function.
> (guix-memoized-defun): New macro.
OK.
Ludo’.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-06-07 15:24 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-06 20:44 [PATCH 1/2] emacs: Add "memoization" code Alex Kost
2015-06-07 15:24 ` Ludovic Courtès
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/guix.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.