From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Using gv in map and seq? Date: Mon, 15 Jun 2015 12:30:03 -0400 Message-ID: References: <87egldxn7p.fsf@petton.fr> <878ublkrqe.fsf@petton.fr> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1434385882 29384 80.91.229.3 (15 Jun 2015 16:31:22 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 15 Jun 2015 16:31:22 +0000 (UTC) Cc: emacs-devel To: Nicolas Petton Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jun 15 18:31:12 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Z4XHy-0006gJ-O5 for ged-emacs-devel@m.gmane.org; Mon, 15 Jun 2015 18:31:10 +0200 Original-Received: from localhost ([::1]:35346 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4XHx-00089l-HC for ged-emacs-devel@m.gmane.org; Mon, 15 Jun 2015 12:31:09 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41586) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4XHN-00082w-JD for emacs-devel@gnu.org; Mon, 15 Jun 2015 12:30:37 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z4XHJ-0008Oy-6W for emacs-devel@gnu.org; Mon, 15 Jun 2015 12:30:33 -0400 Original-Received: from mercure.iro.umontreal.ca ([132.204.24.67]:55276) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4XHJ-0008Oq-2A for emacs-devel@gnu.org; Mon, 15 Jun 2015 12:30:29 -0400 Original-Received: from hidalgo.iro.umontreal.ca (hidalgo.iro.umontreal.ca [132.204.27.50]) by mercure.iro.umontreal.ca (Postfix) with ESMTP id 8DBB585EE6; Mon, 15 Jun 2015 12:30:28 -0400 (EDT) Original-Received: from lechon.iro.umontreal.ca (lechon.iro.umontreal.ca [132.204.27.242]) by hidalgo.iro.umontreal.ca (Postfix) with ESMTP id 98B3A1E5B94; Mon, 15 Jun 2015 12:30:03 -0400 (EDT) Original-Received: by lechon.iro.umontreal.ca (Postfix, from userid 20848) id 70084B4086; Mon, 15 Jun 2015 12:30:03 -0400 (EDT) In-Reply-To: <878ublkrqe.fsf@petton.fr> (Nicolas Petton's message of "Mon, 15 Jun 2015 15:48:09 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) X-DIRO-MailScanner-Information: Please contact the ISP for more information X-DIRO-MailScanner: Found to be clean X-DIRO-MailScanner-SpamCheck: n'est pas un polluriel, SpamAssassin (score=-2.82, requis 5, autolearn=not spam, ALL_TRUSTED -2.82, MC_TSTLAST 0.00) X-DIRO-MailScanner-From: monnier@iro.umontreal.ca X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 132.204.24.67 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:187183 Archived-At: >>> I was wondering if it would make sense to use gv in map or seq? >> Not sure what you mean. Do you mean to add a setter for seq-elt? > Yes I see. Yes, defining a "gv-setter" or "gv-expander" would be good. Oh, wait, no, it's not needed, since seq-elt is an alias for `elt' and that one already has a setter. IOW it already works! > (and a getter too?). When defining a setter, you need to define the getter as well, indeed, but that's generally the trivial part. > The same goes for `map-elt'. This one is not pre-defined, so yes, we should define it. Actually, this just means to turn map-put into a gv-setter. Something like the patch below seems to work (and it generalizes map-put to accept for MAP not only symbols but any "lvalue"). It also tightens the code generated by map--dispatch. Stefan diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el index dd7fb91..26ad6ce 100644 --- a/lisp/emacs-lisp/map.el +++ b/lisp/emacs-lisp/map.el @@ -82,25 +82,19 @@ The following keyword types are meaningful: `:list', An error is thrown if MAP is neither a list, hash-table nor array. -Return RESULT if non-nil or the result of evaluation of the -form. +Return RESULT if non-nil or the result of evaluation of the form. \(fn (VAR MAP [RESULT]) &rest ARGS)" (declare (debug t) (indent 1)) (unless (listp spec) (setq spec `(,spec ,spec))) - (let ((map-var (car spec)) - (result-var (make-symbol "result"))) - `(let ((,map-var ,(cadr spec)) - ,result-var) - (setq ,result-var - (cond ((listp ,map-var) ,(plist-get args :list)) - ((hash-table-p ,map-var) ,(plist-get args :hash-table)) - ((arrayp ,map-var) ,(plist-get args :array)) - (t (error "Unsupported map: %s" ,map-var)))) - ,@(when (cddr spec) - `((setq ,result-var ,@(cddr spec)))) - ,result-var))) + (let ((map-var (car spec))) + `(let* ,(unless (eq map-var (cadr spec)) `((,map-var ,(cadr spec)))) + (cond ((listp ,map-var) ,(plist-get args :list)) + ((hash-table-p ,map-var) ,(plist-get args :hash-table)) + ((arrayp ,map-var) ,(plist-get args :array)) + (t (error "Unsupported map: %s" ,map-var))) + ,@(cddr spec)))) (defun map-elt (map key &optional default) "Perform a lookup in MAP of KEY and return its associated value. @@ -109,27 +103,23 @@ If KEY is not found, return DEFAULT which defaults to nil. If MAP is a list, `equal' is used to lookup KEY. MAP can be a list, hash-table or array." + (declare + (gv-expander + (lambda (do) + (macroexp-let2* nil + ;; Eval them once and for all in the right order. + ((map map) (key key) (default default)) + `(map--dispatch map + :list ,(gv-get `(alist-get ,key ,map ,default) do) + :hash-table ,(funcall do `(gethash ,key ,map ,default) + (lambda (v) `(puthash ,key ,v ,map))) + :array ,(funcall do `(aref ,map ,key) + (lambda (v) `(aset ,map ,key ,v)))))))) (map--dispatch map :list (map--elt-list map key default) :hash-table (gethash key map default) :array (map--elt-array map key default))) -(defmacro map-put (map key value) - "In MAP, associate KEY with VALUE and return MAP. -If KEY is already present in MAP, replace the associated value -with VALUE. - -MAP can be a list, hash-table or array." - (declare (debug t)) - (let ((symbol (symbolp map))) - `(progn - (map--dispatch (m ,map m) - :list (if ,symbol - (setq ,map (cons (cons ,key ,value) m)) - (error "Literal lists are not allowed, %s must be a symbol" ',map)) - :hash-table (puthash ,key ,value m) - :array (aset m ,key ,value))))) - (defmacro map-delete (map key) "In MAP, delete the key KEY if present and return MAP. If MAP is an array, store nil at the index KEY.