unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#28753: 25.3; Functions to get alist from hash table and vice versa
@ 2017-10-09  0:25 Drew Adams
  2017-10-09 13:20 ` Michael Heerdegen
  2017-12-30 21:26 ` Philipp Stephani
  0 siblings, 2 replies; 23+ messages in thread
From: Drew Adams @ 2017-10-09  0:25 UTC (permalink / raw)
  To: 28753

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'





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

end of thread, other threads:[~2022-04-22 15:21 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2017-12-31  0:01   ` Drew Adams
2018-03-04 19:17     ` Philipp Stephani
2018-03-05  0:01       ` Drew Adams

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