all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Andrea Corallo <akrl@sdf.org>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: [PATCH] extend map-into (was: new function proposal alist-to-hash)
Date: Sat, 05 Oct 2019 08:28:36 +0000	[thread overview]
Message-ID: <xjf5zl361fv.fsf_-_@sdf.org> (raw)
In-Reply-To: <jwvmueg2v7d.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Fri, 04 Oct 2019 15:16:13 -0400")

[-- Attachment #1: Type: text/plain, Size: 405 bytes --]

Stefan Monnier <monnier@iro.umontreal.ca> 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

[-- Attachment #2: 0001-Extend-map-into-for-better-control-on-hash-table-cre.patch --]
[-- Type: text/x-patch, Size: 2950 bytes --]

From 2b94c00384f7bd4bfd7b6108e5be3b261e1cf018 Mon Sep 17 00:00:00 2001
From: Andrea Corallo <akrl@sdf.org>
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.
 \f
 * Installation Changes in Emacs 27.1
 
+** 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))
 
-(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 η-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))
 
-(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 `equal'."
+  (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)
-- 
2.7.4


  parent reply	other threads:[~2019-10-05  8:28 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-04  9:58 new function proposal alist-to-hash Andrea Corallo
2019-10-04 19:16 ` Stefan Monnier
2019-10-05  8:18   ` Andrea Corallo
2019-10-05 15:13     ` Stefan Monnier
2019-10-05 15:45       ` Andrea Corallo
2019-10-05  8:28   ` Andrea Corallo [this message]
2019-10-06 14:02     ` [PATCH] extend map-into Stefan Monnier
2019-10-06 20:59       ` Andrea Corallo
2019-10-08 17:29         ` Stefan Monnier
2019-10-08 18:46           ` Andrea Corallo
2019-10-08 20:23             ` Stefan Monnier
2019-10-09 15:35               ` Andrea Corallo
2019-10-09 19:41                 ` Stefan Monnier
2019-10-09 20:02                   ` Andrea Corallo
2019-10-10  8:27                     ` Nicolas Petton
2019-10-10  8:28                     ` Nicolas Petton
2019-10-10 10:06                       ` Andrea Corallo
2019-10-10 11:47                         ` Nicolas Petton
2019-10-11 16:19                         ` Stefan Monnier
2019-10-11 16:29                           ` Andrea Corallo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xjf5zl361fv.fsf_-_@sdf.org \
    --to=akrl@sdf.org \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.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.