From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Andrea Corallo Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] extend map-into Date: Sun, 06 Oct 2019 20:59:50 +0000 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="10695"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (berkeley-unix) Cc: Nicolas Petton , emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Oct 06 23:00:23 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iHDdY-0002UP-Rm for ged-emacs-devel@m.gmane.org; Sun, 06 Oct 2019 23:00:16 +0200 Original-Received: from localhost ([::1]:36978 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iHDdX-0002V6-8m for ged-emacs-devel@m.gmane.org; Sun, 06 Oct 2019 17:00:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37841) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iHDdF-0002Mv-4o for emacs-devel@gnu.org; Sun, 06 Oct 2019 16:59:58 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iHDdD-0005gY-OF for emacs-devel@gnu.org; Sun, 06 Oct 2019 16:59:57 -0400 Original-Received: from mx.sdf.org ([205.166.94.20]:55886) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iHDdD-0005cf-Cl for emacs-devel@gnu.org; Sun, 06 Oct 2019 16:59:55 -0400 Original-Received: from sdf.org (IDENT:akrl@sverige.freeshell.org [205.166.94.5]) by mx.sdf.org (8.15.2/8.14.5) with ESMTPS id x96KxocR007644 (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256 bits) verified NO); Sun, 6 Oct 2019 20:59:50 GMT Original-Received: (from akrl@localhost) by sdf.org (8.15.2/8.12.8/Submit) id x96Kxovm004924; Sun, 6 Oct 2019 20:59:50 GMT In-Reply-To: (Stefan Monnier's message of "Sun, 06 Oct 2019 10:02:54 -0400") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 205.166.94.20 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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" Xref: news.gmane.org gmane.emacs.devel:240676 Archived-At: --=-=-= Content-Type: text/plain Stefan Monnier writes: > [ I added Nicolas explicitly in the list, in case he's too busy to read > all messages. ] > >> would a solution like the one in attached patch do the job? > > Yes and no: it does the job, but only for `map-into`. So that doesn't > immediately cover the needs of `map-merge` and other callers of > `map-into` (e.g. the `map-values-apply` I suggested which would return > a new map). > > I guess we could use a `type` that's not a symbol but a list (whose > head is a symbol and the rest are the args), but it could be a bit > inconvenient (and would impact efficiency as well because it makes the > cl-generic dispatch more complex). Cool, is the attached patch doing what you suggest? > On a related note, maybe it would be good to have map.el primitives that > let you create a new map of the same type as another (preserving :test). > >> + (let* ((h-args (if keyword-args >> + keyword-args >> + (list :size (map-length map) :test 'equal))) > > Aka (or keyword-args (list :size (map-length map) :test 'equal)) > ops :D > Stefan > Bests Andrea -- akrl@sdf.org --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Extend-map-into-for-better-control-on-hash-table-cre.patch >From 55780be0af0e8443fb9b14b77cb3d13ca9af75fb Mon Sep 17 00:00:00 2001 From: Andrea Corallo Date: Sun, 6 Oct 2019 22:43:11 +0200 Subject: [PATCH] Extend 'map-into' for better control on hash table creation. --- etc/NEWS | 4 ++++ lisp/emacs-lisp/map.el | 29 +++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index c8cc753..5942a32 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -24,6 +24,10 @@ applies, and please also update docstrings as needed. * Installation Changes in Emacs 27.1 +** Extend function 'map-into'. +Add a 'map-into' method to have hash table creation parameters +tweak-able. + ** Emacs now uses GMP, the GNU Multiple Precision library. By default, if 'configure' does not find a suitable libgmp, it arranges for the included mini-gmp library to be built and used. diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el index 54e802e..dfc1500 100644 --- a/lisp/emacs-lisp/map.el +++ b/lisp/emacs-lisp/map.el @@ -338,7 +338,8 @@ map-every-p t)) (defun map-merge (type &rest maps) - "Merge into a map of type TYPE all the key/value pairs in MAPS." + "Merge into a map of type TYPE all the key/value pairs in MAPS. +See `map-into' for all supported values of TYPE." (let ((result (map-into (pop maps) type))) (while maps ;; FIXME: When `type' is `list', we get an O(N^2) behavior. @@ -354,7 +355,8 @@ map-merge-with "Merge into a map of type TYPE all the key/value pairs in MAPS. When two maps contain the same key (`eql'), call FUNCTION on the two values and use the value returned by it. -MAP can be a list, hash-table or array." +MAP can be a list, hash-table or array. +See `map-into' for all supported values of TYPE." (let ((result (map-into (pop maps) type)) (not-found (cons nil nil))) (while maps @@ -458,17 +460,28 @@ map-do (funcall function index elt)) array)) -(cl-defmethod map-into (map (_type (eql hash-table))) - "Convert MAP into a hash-table." - ;; FIXME: Just knowing we want a hash-table is insufficient, since that - ;; doesn't tell us the test function to use with it! - (let ((ht (make-hash-table :size (map-length map) - :test 'equal))) +(defsubst map--into-hash (map keyword-args) + "Convert MAP into a hash-table. +KEYWORD-ARGS are forwarded to `make-hash-table'." + (let ((ht (apply #'make-hash-table keyword-args))) (map-apply (lambda (key value) (setf (map-elt ht key) value)) map) ht)) +(cl-defmethod map-into (map (_type (eql hash-table))) + "Convert MAP into a hash-table." + (map--into-hash map (list :size (map-length map) :test 'equal))) + +(cl-defmethod map-into (map (args (head hash-table))) + "Convert MAP into a hash-table. +ARGS is a list where the car is 'hash-table' and the cdr are the keyword-args + forwarded to `make-hash-table'. + +Example: + (map-into '((1 . 3)) '(hash-table :test eql))" + (map--into-hash map (cdr args))) + (defun map--make-pcase-bindings (args) "Return a list of pcase bindings from ARGS to the elements of a map." (seq-map (lambda (elt) -- 2.7.4 --=-=-=--