From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Philipp Stephani Newsgroups: gmane.emacs.bugs Subject: bug#28753: 25.3; Functions to get alist from hash table and vice versa Date: Sat, 30 Dec 2017 21:26:47 +0000 Message-ID: References: <54ecd1bb-0c84-4b0a-b19e-3a89cbe832bc@default> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="94eb2c122b70afd132056195686d" X-Trace: blaine.gmane.org 1514669184 10393 195.159.176.226 (30 Dec 2017 21:26:24 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 30 Dec 2017 21:26:24 +0000 (UTC) Cc: 28753@debbugs.gnu.org To: Drew Adams Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Dec 30 22:26:19 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVOdx-0001xP-SI for geb-bug-gnu-emacs@m.gmane.org; Sat, 30 Dec 2017 22:26:14 +0100 Original-Received: from localhost ([::1]:40672 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eVOfw-0002vu-PK for geb-bug-gnu-emacs@m.gmane.org; Sat, 30 Dec 2017 16:28:16 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46243) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eVOfn-0002up-Ag for bug-gnu-emacs@gnu.org; Sat, 30 Dec 2017 16:28:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eVOfj-000706-BY for bug-gnu-emacs@gnu.org; Sat, 30 Dec 2017 16:28:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:49508) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eVOfj-0006zc-7M for bug-gnu-emacs@gnu.org; Sat, 30 Dec 2017 16:28:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eVOfj-0000Ah-2G for bug-gnu-emacs@gnu.org; Sat, 30 Dec 2017 16:28:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Philipp Stephani Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 30 Dec 2017 21:28:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 28753 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 28753-submit@debbugs.gnu.org id=B28753.1514669224541 (code B ref 28753); Sat, 30 Dec 2017 21:28:03 +0000 Original-Received: (at 28753) by debbugs.gnu.org; 30 Dec 2017 21:27:04 +0000 Original-Received: from localhost ([127.0.0.1]:58184 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVOem-00008f-Hc for submit@debbugs.gnu.org; Sat, 30 Dec 2017 16:27:04 -0500 Original-Received: from mail-qt0-f179.google.com ([209.85.216.179]:40066) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVOel-00008A-DY for 28753@debbugs.gnu.org; Sat, 30 Dec 2017 16:27:03 -0500 Original-Received: by mail-qt0-f179.google.com with SMTP id u42so57494366qte.7 for <28753@debbugs.gnu.org>; Sat, 30 Dec 2017 13:27:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Ke2WB5kQmS9U1SF2ToK5PLalj+Zs7UTFQhgcMVTW3LI=; b=TN2Be83tVSrJ4ROvYuMTeFqdfPgRhaY9qHdGITs6gZ8QhO44C1OHYC9yk53Mz99GDC nAI2AtyLW9sPA2RpiPqLL1unsIp3aSeWLXot6JbG+aonjruiKmDwG8EqygDg9cKFuo+R 49pxZcxvtbC6lLD0JeT9LZYQPDNdzXs6LxbSMyDifiNzwAAdi8E0YRDfoKSzc/5Fnxbf +RZtYVw8/tzjRh7P/I8PyM5XeDKorV0VdXZy8eywXP6T94INnYicNlmhckfFfLjskebN muyinXbSZII0d8hQbS+/Qn+kvUx3cAKzDGw0HtQzeYtZLiStj1K2ZA+LqlYT59qvns/G Zhdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Ke2WB5kQmS9U1SF2ToK5PLalj+Zs7UTFQhgcMVTW3LI=; b=UZ3bG5fNvmTRd4PgJtZcjofgZMTe6H9U9T/+PGXd+2itpiBwgm9Dp4kPHHr1z8+jE4 f8FAXpIdwEHuX0xwzZ/+lAGX+akiY7DK3nmSvYpr/U2lPKxkLPc+wnwRbA84RlRQkBpQ uM2petbJHH+BOXoeBzlQ039WSbugpC7fA1RecK8k4MQ3KBy5JKr4GhUJb/ork7sccCWn rmR/NFo8734rVqH4B707TKd+IUXyyWsCYs1PdIxk4xKa8GnHoS27m5ty4kj/xTVG8FEy Xf2wmBSXbXZ8hiHbPNfWYEYiJyr9vTxiiaF64cTuIk1/BNylJoALTeWAyGw9uzz2Kl9+ Y2jw== X-Gm-Message-State: AKGB3mLwNVPVej3epMMelkZxBeFE24A/YBNHMCe/Ua2KQVgAmjPfJES0 TJlI02HQjQKzT5ltx262k4PbnFsujnZ2Gbn3YtE= X-Google-Smtp-Source: ACJfBougU4kw1i2nPf4whchphpwZr9pddURV6CqbHwZQi7tU3v09WGEU7pmkoJZLlnpbQ/5kORwuj9gSFyXctzJwSF0= X-Received: by 10.200.27.76 with SMTP id p12mr54411736qtk.310.1514669217925; Sat, 30 Dec 2017 13:26:57 -0800 (PST) In-Reply-To: <54ecd1bb-0c84-4b0a-b19e-3a89cbe832bc@default> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:141632 Archived-At: --94eb2c122b70afd132056195686d Content-Type: text/plain; charset="UTF-8" Drew Adams schrieb am Mo., 9. Okt. 2017 um 02:27 Uhr: > Dunno whether functions like these might be useful. I use something > similar. If you think they're useful, consider adding them. > I think both are useful. > > (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))) > This doesn't work if the value is nil. You need to use an uninterned symbol or some other unique object, e.g. (eq (gethash key ht #1='#:void) #1#) > (puthash key val ht))) > ht)) > I'd personally make use-last-p another keyword argument, though. > > (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))) > > Hmm, is the order guaranteed? I haven't found anything in the Emacs Lisp manual about this, so maybe just leave out the parenthetical remark or say that the order is unspecified? --94eb2c122b70afd132056195686d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


Drew A= dams <drew.adams@oracle.com= > schrieb am Mo., 9. Okt. 2017 um 02:27=C2=A0Uhr:
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.

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.
(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
--94eb2c122b70afd132056195686d--