From: Philipp Stephani <p.stephani2@gmail.com>
To: Drew Adams <drew.adams@oracle.com>
Cc: 28753@debbugs.gnu.org
Subject: bug#28753: 25.3; Functions to get alist from hash table and vice versa
Date: Sat, 30 Dec 2017 21:26:47 +0000 [thread overview]
Message-ID: <CAArVCkSjR-o9au-gkUzKm21zO9Nn0y-dH=w8YiP=35Hp=UXNhw@mail.gmail.com> (raw)
In-Reply-To: <54ecd1bb-0c84-4b0a-b19e-3a89cbe832bc@default>
[-- Attachment #1: Type: text/plain, Size: 1989 bytes --]
Drew Adams <drew.adams@oracle.com> schrieb am Mo., 9. Okt. 2017 um
02:27 Uhr:
> Dunno whether functions like these might be useful. I use something
> similar. If you think they're useful, consider adding them.
>
I think both are useful.
>
> (cl-defun alist-to-hash-table (alist &optional use-last-p
> &key (test 'eql) weakness (size 65)
> (rehash-size 1.5) (rehash-threshold
> 0.8))
> "Create and return a hash table created from ALIST.
> By default, if the same alist key is used in more than one alist entry
> then only the first entry is used for the hash table. Non-nil
> USE-LAST-P means override this to use only the last entry for a given
> key.
>
> See `make-hash-table' for the keyword arguments you can use and their
> default values."
> (let ((ht (make-hash-table :test test :weakness weakness :size size
> :rehash-size rehash-size :rehash-threshold
> rehash-threshold))
> key val)
> (dolist (key.val alist)
> (setq key (car key.val)
> val (cdr key.val))
> (when (or use-last-p (not (gethash key ht)))
>
This doesn't work if the value is nil. You need to use an uninterned symbol
or some other unique object, e.g.
(eq (gethash key ht #1='#:void) #1#)
> (puthash key val ht)))
> ht))
>
I'd personally make use-last-p another keyword argument, though.
>
> (defun hash-table-to-alist (hash-table)
> "Create and return an alist created from HASH-TABLE.
> The order of alist entries is the same as the order of hash-table
> entries (which normally is the order in which the entries were added
> to the table)."
> (let ((al ()))
> (maphash (lambda (key val) (push (cons key val) al)) hash-table)
> (nreverse al)))
>
>
Hmm, is the order guaranteed? I haven't found anything in the Emacs Lisp
manual about this, so maybe just leave out the parenthetical remark or say
that the order is unspecified?
[-- Attachment #2: Type: text/html, Size: 2967 bytes --]
next prev parent reply other threads:[~2017-12-30 21:26 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-09 0:25 bug#28753: 25.3; Functions to get alist from hash table and vice versa Drew Adams
2017-10-09 13:20 ` Michael Heerdegen
2017-10-09 14:11 ` Drew Adams
2017-10-11 16:42 ` Drew Adams
[not found] ` <87wp4038m0.fsf@web.de>
2017-10-12 13:27 ` Nicolas Petton
2017-10-12 13:46 ` Michael Heerdegen
2017-10-12 14:36 ` Drew Adams
2017-11-06 16:19 ` Drew Adams
2017-11-07 0:46 ` Noam Postavsky
2017-11-07 2:24 ` Drew Adams
2017-11-07 2:51 ` Noam Postavsky
2017-11-07 13:28 ` Michael Heerdegen
2017-12-30 20:40 ` Philipp Stephani
2017-12-30 21:08 ` Drew Adams
2017-12-30 21:15 ` Philipp Stephani
2017-10-12 15:56 ` Noam Postavsky
2017-10-12 13:30 ` Nicolas Petton
2022-04-22 13:18 ` Lars Ingebrigtsen
2022-04-22 15:21 ` Drew Adams
2017-12-30 21:26 ` Philipp Stephani [this message]
2017-12-31 0:01 ` Drew Adams
2018-03-04 19:17 ` Philipp Stephani
2018-03-05 0:01 ` Drew Adams
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='CAArVCkSjR-o9au-gkUzKm21zO9Nn0y-dH=w8YiP=35Hp=UXNhw@mail.gmail.com' \
--to=p.stephani2@gmail.com \
--cc=28753@debbugs.gnu.org \
--cc=drew.adams@oracle.com \
/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.