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: [PATCH] extend map-into (was: new function proposal alist-to-hash) Date: Sat, 05 Oct 2019 08:28:36 +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="253241"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (berkeley-unix) Cc: emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Oct 05 10:29:11 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 1iGfR8-0013jf-Uq for ged-emacs-devel@m.gmane.org; Sat, 05 Oct 2019 10:29:11 +0200 Original-Received: from localhost ([::1]:54792 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iGfR7-00046v-NW for ged-emacs-devel@m.gmane.org; Sat, 05 Oct 2019 04:29:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59020) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iGfQd-00046b-VY for emacs-devel@gnu.org; Sat, 05 Oct 2019 04:28:41 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iGfQb-0008Hy-Rt for emacs-devel@gnu.org; Sat, 05 Oct 2019 04:28:39 -0400 Original-Received: from ol.sdf.org ([205.166.94.20]:59409 helo=mx.sdf.org) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iGfQb-0008Hn-J0 for emacs-devel@gnu.org; Sat, 05 Oct 2019 04:28:37 -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 x958Sa6f017597 (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256 bits) verified NO); Sat, 5 Oct 2019 08:28:36 GMT Original-Received: (from akrl@localhost) by sdf.org (8.15.2/8.12.8/Submit) id x958Sai7003059; Sat, 5 Oct 2019 08:28:36 GMT In-Reply-To: (Stefan Monnier's message of "Fri, 04 Oct 2019 15:16:13 -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:240604 Archived-At: --=-=-= Content-Type: text/plain Stefan Monnier writes: > Yes, this is a very serious limitation of `map-into`. > I decided not to try to tackle it when I converted map.el to use > cl-defmethod, but I'd welcome help on this. > > > Stefan Hi, would a solution like the one in attached patch do the job? I've added a rest arg so that the current code should not be impacted. Andrea -- akrl@sdf.org --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0001-Extend-map-into-for-better-control-on-hash-table-cre.patch Content-Transfer-Encoding: quoted-printable >From 2b94c00384f7bd4bfd7b6108e5be3b261e1cf018 Mon Sep 17 00:00:00 2001 From: Andrea Corallo Date: Sat, 5 Oct 2019 09:38:13 +0200 Subject: [PATCH] Extend 'map-into' for better control on hash table creation --- etc/NEWS | 5 +++++ lisp/emacs-lisp/map.el | 22 ++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index c8cc753..caea917 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -24,6 +24,11 @@ applies, and please also update docstrings as needed. * Installation Changes in Emacs 27.1 =20 +** Extend function 'map-into'. +Change 'map-into' lambda-list into (map type &rest args). +When 'map-into' is used to create hash tables args are now forwarded to +'make-hash-table' for better control. + ** 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..7360705 100644 --- a/lisp/emacs-lisp/map.el +++ b/lisp/emacs-lisp/map.el @@ -367,12 +367,12 @@ map-merge-with (pop maps))) result)) =20 -(cl-defgeneric map-into (map type) +(cl-defgeneric map-into (map type &rest args) "Convert the map MAP into a map of type TYPE.") ;; FIXME: I wish there was a way to avoid this =CE=B7-redex! -(cl-defmethod map-into (map (_type (eql list))) (map-pairs map)) -(cl-defmethod map-into (map (_type (eql alist))) (map-pairs map)) -(cl-defmethod map-into (map (_type (eql plist))) +(cl-defmethod map-into (map (_type (eql list)) &rest _) (map-pairs map)) +(cl-defmethod map-into (map (_type (eql alist)) &rest _) (map-pairs map)) +(cl-defmethod map-into (map (_type (eql plist)) &rest _) (let ((plist '())) (map-do (lambda (k v) (setq plist `(,k ,v ,@plist))) map) plist)) @@ -458,12 +458,14 @@ map-do (funcall function index elt)) array)) =20 -(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))) +(cl-defmethod map-into (map (_type (eql hash-table)) &rest keyword-args) + "Convert MAP into a hash-table. +If KEYWORD-ARGS are provided they are forwarded to `make-hash-table'. +If KEYWORD-ARGS is nil hash table size is set to MAP length and test to `e= qual'." + (let* ((h-args (if keyword-args + keyword-args + (list :size (map-length map) :test 'equal))) + (ht (apply #'make-hash-table h-args))) (map-apply (lambda (key value) (setf (map-elt ht key) value)) map) --=20 2.7.4 --=-=-=--