unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: new function proposal alist-to-hash
@ 2019-10-04  9:58 Andrea Corallo
  2019-10-04 19:16 ` Stefan Monnier
  0 siblings, 1 reply; 24+ messages in thread
From: Andrea Corallo @ 2019-10-04  9:58 UTC (permalink / raw)
  To: akrl; +Cc: Stefan Monnier, emacs-devel

I just wanted elaborate a little more on the following two points:

- I think is quite useful to be able to create in a concise and
  explicit way nested hash tables. This is a common feature of many
  "modern" languages.
  Here both solutions compared:

(alist-to-hash '((a . x)
		 (b . ((i . j)
		       (k . l)))
		 (c . y)))

(map-into `((a . x)
	    (b . ,(map-into '((i . j)
			      (k . l))
			    'hash-table))
	    (c . y))
	  'hash-table)

- map-into does not let you tweak make-hash-table parameters.
  This is especially a limitation regarding :test so is effectively a
  solution to say ~50% of the use cases.

Bests
  Andrea

--
akrl@sdf.org



^ permalink raw reply	[flat|nested] 24+ messages in thread
* new function proposal alist-to-hash
@ 2019-10-03 21:25 Andrea Corallo
  2019-10-03 21:40 ` Drew Adams
  2019-10-03 21:56 ` Stefan Monnier
  0 siblings, 2 replies; 24+ messages in thread
From: Andrea Corallo @ 2019-10-03 21:25 UTC (permalink / raw)
  To: emacs-devel

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

Hi all,
I'd like to propose a new simple function called alist-to-hash to make
hash-tables from a-lists.
In case something equivalent already exists I apologize.
Please be patient, first contribution here :)

I attach the patch.

Best Regards
  Andrea

PS Copyright paperwork are done.
-- 
akrl@sdf.org

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-new-function-to-make-hash-tables-from-a-lists.patch --]
[-- Type: text/x-patch, Size: 1730 bytes --]

From e2bca29d6fca8c1a40e57526432cb4ce29b2245b Mon Sep 17 00:00:00 2001
From: Andrea Corallo <akrl@sdf.org>
Date: Thu, 3 Oct 2019 22:56:43 +0200
Subject: [PATCH] Add new function to make hash-tables from a-lists

* lisp/emacs-lisp/subr-x.el (alist-to-hash):
  New function make hash-tables from a-lists.
* etc/NEWS: Announce it.
---
 etc/NEWS                  |  3 +++
 lisp/emacs-lisp/subr-x.el | 12 ++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index c8cc753..f4924ba 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -24,6 +24,9 @@ applies, and please also update docstrings as needed.
 \f
 * Installation Changes in Emacs 27.1

+** New function 'alist-to-hash'.
+Create recursively hash-tables from a-lists.
+
 ** 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/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 3da5241..e67f59b 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -204,6 +204,18 @@ hash-table-values
   "Return a list of values in HASH-TABLE."
   (cl-loop for v being the hash-values of hash-table collect v))

+(defun alist-to-hash (a-list &rest keyword-args)
+  "Create recursively an hash table from A-LIST.
+KEYWORD-ARGS parameters are forwarded to `make-hash-table'."
+  (cl-loop with h = (apply #'make-hash-table keyword-args)
+	   for (k . v) in a-list
+	   do (puthash k
+		       (if (consp v)
+			   (apply #'alist-to-hash v keyword-args)
+			 v)
+		       h)
+	   finally (return h)))
+
 (defsubst string-empty-p (string)
   "Check whether STRING is empty."
   (string= string ""))
--
2.7.4

^ permalink raw reply related	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2019-10-11 16:29 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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   ` [PATCH] extend map-into (was: new function proposal alist-to-hash) Andrea Corallo
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
  -- strict thread matches above, loose matches on Subject: below --
2019-10-03 21:25 new function proposal alist-to-hash Andrea Corallo
2019-10-03 21:40 ` Drew Adams
2019-10-03 21:56 ` Stefan Monnier
2019-10-03 22:07   ` Andrea Corallo

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).