From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Philipp Stephani
Dunno whether functions like these might be useful.=C2=A0= I use something
similar.=C2=A0 If you think they're useful, consider adding them.
= blockquote>I think both are useful.=C2=A0
(cl-defun alist-to-hash-table (alist &optional use-last-p
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&key (test &= #39;eql) weakness (size 65)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(rehash-size 1.5= ) (rehash-threshold 0.8))
=C2=A0 "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.=C2=A0 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."
=C2=A0 (let ((ht=C2=A0 (make-hash-table :test test :weakness weakness :size= size
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 :rehash-size rehash-size :rehash-threshold = rehash-threshold))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 key val)
=C2=A0 =C2=A0 (dolist (key.val=C2=A0 alist)
=C2=A0 =C2=A0 =C2=A0 (setq key=C2=A0 (car key.val)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 val=C2=A0 (cdr key.val))
=C2=A0 =C2=A0 =C2=A0 (when (or use-last-p=C2=A0 (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.--94eb2c122b70afd132056195686d--(eq (gethash key ht #1=3D'#:void) #1#)=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 (puthash key val ht)))
=C2=A0 =C2=A0 ht))I'd personally m= ake use-last-p another keyword argument, though.=C2=A0
(defun hash-table-to-alist (hash-table)
=C2=A0 "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)."
=C2=A0 (let ((al=C2=A0 ()))
=C2=A0 =C2=A0 (maphash (lambda (key val) (push (cons key val) al)) hash-tab= le)
=C2=A0 =C2=A0 (nreverse al)))
Hmm, is the order guaranteed? I haven'= t found anything in the Emacs Lisp manual about this, so maybe just leave o= ut the parenthetical remark or say that the order is unspecified?=C2=A0