emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

hash-table-{to, from}-alist (was: print hash table to disk and reread in


From: Ted Zlatanov
Subject: hash-table-{to, from}-alist (was: print hash table to disk and reread in hash table)
Date: Wed, 19 Nov 2008 15:37:46 -0600
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux)

On Mon, 17 Nov 2008 11:15:46 -0600 Ted Zlatanov <address@hidden> wrote: 

...
TZ> I propose the following functions (originals were contributed to Gnus by
TZ> Andreas Fuchs <address@hidden>):

TZ> (defun hash-table-to-alist (hash)
TZ>   "Build an alist from the values in HASH."
TZ>   (let ((list nil))
TZ>     (maphash
TZ>      (lambda (key value)
TZ>        (setq list (cons (cons key value) list)))
TZ>      hash)
TZ>     list))

TZ> ;; this would take the usual make-hash-table arguments (:test, :size,
TZ> ;; :rehash-size, :rehash-threshold, :weakness) so those don't have to
TZ> ;; get serialized with the alist.  This makes the implementation much
TZ> ;; simpler, complicating life slightly for the API consumers

TZ> ;; untested

TZ> (defun hash-table-from-alist (alist &rest options)
TZ>   "Build a hashtable from the values in ALIST."
TZ>   (let ((ht (apply 'make-hash-table options)))
TZ>     (mapc
TZ>      (lambda (kv-pair)
TZ>        (puthash (car kv-pair) (cdr kv-pair) ht))
TZ>      alist)
TZ>      ht))

Even though the functions here are not by any means perfect (for
instance, a hashtable containing another hashtable will not be converted
correctly) I think they should go into Emacs.  No one has commented yet.

I can address the nested hashtable problem by recursion, but it won't
work when converting back into a hashtable because I won't know if the
original data was a hashtable or an alist.  Please let me know if
there's a better way that preserves the clean hashtable-to-alist
mapping (maybe I have to include metadata, after all, which I was hoping
to avoid).

If there are no objections or comments, I'll commit my version tomorrow
with a manual update to follow once the functions are stable.  Where
should this go under the lisp/ directory?

Thanks
Ted





reply via email to

[Prev in Thread] Current Thread [Next in Thread]