From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.devel 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 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <867i6z1jo5.fsf_-_@lifelogs.com> References: <34f9604c-a23b-4ad9-9c84-f45884a6df23@x16g2000prn.googlegroups.com> <86od3dfd86.fsf@lifelogs.com> <868wuflxv9.fsf@lifelogs.com> <863aknitfg.fsf@lifelogs.com> <20080830051807.GB9625@tomas> <86bpwe9su5.fsf@lifelogs.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1227130725 16081 80.91.229.12 (19 Nov 2008 21:38:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 19 Nov 2008 21:38:45 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Nov 19 22:39:47 2008 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1L2uls-0005Vh-18 for ged-emacs-devel@m.gmane.org; Wed, 19 Nov 2008 22:39:36 +0100 Original-Received: from localhost ([127.0.0.1]:52868 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L2ukj-00007x-Bg for ged-emacs-devel@m.gmane.org; Wed, 19 Nov 2008 16:38:25 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1L2ukO-0008T1-O0 for emacs-devel@gnu.org; Wed, 19 Nov 2008 16:38:04 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1L2ukL-0008RX-LJ for emacs-devel@gnu.org; Wed, 19 Nov 2008 16:38:04 -0500 Original-Received: from [199.232.76.173] (port=55773 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L2ukL-0008RT-Ii for emacs-devel@gnu.org; Wed, 19 Nov 2008 16:38:01 -0500 Original-Received: from main.gmane.org ([80.91.229.2]:40371 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1L2ukL-0004OH-Fo for emacs-devel@gnu.org; Wed, 19 Nov 2008 16:38:01 -0500 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1L2ukE-0002rI-B7 for emacs-devel@gnu.org; Wed, 19 Nov 2008 21:37:54 +0000 Original-Received: from 38.98.147.130 ([38.98.147.130]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 19 Nov 2008 21:37:54 +0000 Original-Received: from tzz by 38.98.147.130 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 19 Nov 2008 21:37:54 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 48 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 38.98.147.130 X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux) Cancel-Lock: sha1:jFcw5fm68fAUy5009qaLr8Zhi1o= X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:105827 Archived-At: On Mon, 17 Nov 2008 11:15:46 -0600 Ted Zlatanov wrote: ... TZ> I propose the following functions (originals were contributed to Gnus by TZ> Andreas Fuchs ): 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