From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alex Kost Subject: [PATCH 1/2] emacs: Add "memoization" code. Date: Sat, 06 Jun 2015 23:44:33 +0300 Message-ID: <87oakszhvy.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55740) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z1KxM-0000OT-OU for guix-devel@gnu.org; Sat, 06 Jun 2015 16:44:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z1KxH-00073S-Em for guix-devel@gnu.org; Sat, 06 Jun 2015 16:44:40 -0400 Received: from mail-lb0-x235.google.com ([2a00:1450:4010:c04::235]:34086) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z1KxH-00073L-6t for guix-devel@gnu.org; Sat, 06 Jun 2015 16:44:35 -0400 Received: by lbcmx3 with SMTP id mx3so62284484lbc.1 for ; Sat, 06 Jun 2015 13:44:34 -0700 (PDT) Received: from leviafan ([217.107.192.168]) by mx.google.com with ESMTPSA id oq2sm2818257lbb.34.2015.06.06.13.44.32 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Jun 2015 13:44:33 -0700 (PDT) List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org To: guix-devel@gnu.org --=-=-= Content-Type: text/plain Some auxiliary code for the next patch. --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0001-emacs-Add-memoization-code.patch Content-Transfer-Encoding: quoted-printable >From 8f6dfdc793d39b40bf57fbc7728e815b40b7c199 Mon Sep 17 00:00:00 2001 From: Alex Kost 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 -*- =20 -;; Copyright =C2=A9 2014 Alex Kost +;; Copyright =C2=A9 2014, 2015 Alex Kost =20 ;; This file is part of GNU Guix. =20 @@ -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)) =20 + +;;; 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) =20 ;;; guix-utils.el ends here --=20 2.2.1 --=-=-=--