unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Andrea Corallo <akrl@sdf.org>
To: emacs-devel@gnu.org
Subject: new function proposal alist-to-hash
Date: Thu, 03 Oct 2019 21:25:36 +0000	[thread overview]
Message-ID: <xjfsgo9ldwv.fsf@sdf.org> (raw)

[-- 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

             reply	other threads:[~2019-10-03 21:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-03 21:25 Andrea Corallo [this message]
2019-10-03 21:40 ` new function proposal alist-to-hash Drew Adams
2019-10-03 21:56 ` Stefan Monnier
2019-10-03 22:07   ` Andrea Corallo
  -- strict thread matches above, loose matches on Subject: below --
2019-10-04  9:58 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

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=xjfsgo9ldwv.fsf@sdf.org \
    --to=akrl@sdf.org \
    --cc=emacs-devel@gnu.org \
    /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 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).