unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: 28753@debbugs.gnu.org
Subject: bug#28753: 25.3; Functions to get alist from hash table and vice versa
Date: Sun, 8 Oct 2017 17:25:47 -0700 (PDT)	[thread overview]
Message-ID: <54ecd1bb-0c84-4b0a-b19e-3a89cbe832bc@default> (raw)

Dunno whether functions like these might be useful.  I use something
similar.  If you think they're useful, consider adding them.

(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)))
        (puthash key val ht)))
    ht))

(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)))


In GNU Emacs 25.3.1 (x86_64-w64-mingw32)
 of 2017-09-26
Windowing system distributor `Microsoft Corp.', version 6.1.7601
Configured using:
 `configure --without-dbus --without-compress-install 'CFLAGS=-O2
 -static -g3' PKG_CONFIG_PATH=/mingw64/lib/pkgconfig'





             reply	other threads:[~2017-10-09  0:25 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-09  0:25 Drew Adams [this message]
2017-10-09 13:20 ` bug#28753: 25.3; Functions to get alist from hash table and vice versa 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
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

  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=54ecd1bb-0c84-4b0a-b19e-3a89cbe832bc@default \
    --to=drew.adams@oracle.com \
    --cc=28753@debbugs.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).