From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Daniel Hartwig Newsgroups: gmane.lisp.guile.user Subject: Re: Hash table read syntax in guile 2.0 Date: Sun, 17 Feb 2013 19:59:41 +0800 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary=14dae9340fe1f9aaae04d5ea575b X-Trace: ger.gmane.org 1361102811 11687 80.91.229.3 (17 Feb 2013 12:06:51 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 17 Feb 2013 12:06:51 +0000 (UTC) Cc: guile-user To: Hengqing Hu Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Feb 17 13:07:13 2013 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1U731T-0002R5-Qg for guile-user@m.gmane.org; Sun, 17 Feb 2013 13:07:12 +0100 Original-Received: from localhost ([::1]:40539 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7319-0006eF-RJ for guile-user@m.gmane.org; Sun, 17 Feb 2013 07:06:51 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:46600) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7311-0006dx-TX for guile-user@gnu.org; Sun, 17 Feb 2013 07:06:47 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U730w-0000xY-Uq for guile-user@gnu.org; Sun, 17 Feb 2013 07:06:43 -0500 Original-Received: from mail-ie0-x22d.google.com ([2607:f8b0:4001:c03::22d]:48118) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U730w-0000xA-OZ for guile-user@gnu.org; Sun, 17 Feb 2013 07:06:38 -0500 Original-Received: by mail-ie0-f173.google.com with SMTP id 9so6378296iec.18 for ; Sun, 17 Feb 2013 04:06:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=teDXsgNdJjR4ZcvqA4yyieLMd9TaeD/C/2LZVbQ0fJA=; b=DEU/4eRuy/g4bngu9I+XC0Cd9ZZVYfzIU5qomFKx4Cjks2zoHoxwKukYUmGBRS1MP8 FPQkimc8C8PZme4X3GhMwmFUf/mhTeE6XSGVrAUEHePjk1PAMIjLzw2QZ3vyqYdUdxwc nbFlPOtogm7U/qYaZVgWmz8gZP7uItQpFrcRweOrds4oFTae8UXfqcbvrk3UcXPcBIbX 9KIeYPX/ngdq5RDwsCDB6tIgy7i1gLvgWxrfQABlqvzbA3kid3cmSZHAbaOzXK5djSmk L8yh6p74pE/2XYdG72JOMtA6iHLtrvdC3itjQCmfKDj4Jyo436DdE5a4LbYUtdq9xO2M HLwQ== X-Received: by 10.50.187.225 with SMTP id fv1mr5792737igc.96.1361102381885; Sun, 17 Feb 2013 03:59:41 -0800 (PST) Original-Received: by 10.64.76.51 with HTTP; Sun, 17 Feb 2013 03:59:41 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4001:c03::22d X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:10114 Archived-At: --14dae9340fe1f9aaae04d5ea575b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 17 February 2013 11:55, Hengqing Hu wrote: > Dear list, > > It seems the read syntax of a hash table has been silently changed. > > Now (make-hash-table) produces > # > instead what was in 1.8 > # > > But the document is not updated yet? > Hi That's how hash-table values are printed, true, but it is not _read syntax_. Neither is it a documented format (although it does appear in the example output of one command). You can not rely on the format of that string to be anything, it is just a convenience. :-) The manual recommends to use hash-fold for counting the elements: (hash-fold (lambda (key value count) (1+ count)) 0 table) You could also adapt your parser to be more flexible, looking only at the tail of the string after the last whitespace. Although such hacks work, it is not advisable. > It would be more convinient if a hash-length procedure could be provided. Right. The two modules (rnrs hashtables) and (srfi srfi-69) both provide *-size. Their interfaces are slightly different to the native hash-tables, and the objects produced are not compatible with each other. Not sure why the native tables don't have an equivalent, perhaps because: - the terms =E2=80=98size=E2=80=99 and =E2=80=98length=E2=80=99 are not so = good for hash tables [1]; - it's very easy to do with hash-fold; - usually more interesting to count particular items rather than everything= ; and - there may not be any guarantees on uniqueness of keys amongst the elements stored in hash table, so what is meant by =E2=80=9Cnumber of elements=E2=80=9D can vary between applications [2]? Hence, hash-fold is always preferred to determine this. However, for quick and dirty case it's simple enough to write -count, so I attach a patch for this with total count in constant time: -- Scheme Procedure: hash-count pred table -- C Function: scm_hash_count (pred, table) Return the number of elements in the given hash TABLE for which `(PRED KEY VALUE)' returns true. If PRED itself is `#f', return the total number of elements. Regards [1] Internally and in the documentation, =E2=80=98size=E2=80=99 is already = associated with the number of buckets. This is consistent with how some academic literature uses the term also. [2] For example, a vhash can store multiple elements with the same key and provides access to them. --14dae9340fe1f9aaae04d5ea575b Content-Type: application/octet-stream; name="0001-add-hash-count-for-native-tables.patch" Content-Disposition: attachment; filename="0001-add-hash-count-for-native-tables.patch" Content-Transfer-Encoding: base64 X-Attachment-Id: f_hda4zqrr0 RnJvbSBlYzUyNDE2YzIwNjBjZmYxMWQwY2I0MzVkZjQ5MjA3ZTVhYTNkYzdhIE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBEYW5pZWwgSGFydHdpZyA8bWFuZHlrZUBnbWFpbC5jb20+CkRh dGU6IFN1biwgMTcgRmViIDIwMTMgMTY6Mzg6MzEgKzA4MDAKU3ViamVjdDogW1B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3QgYi90ZXN0LXN1aXRlL3Rlc3RzL2hhc2gudGVzdAppbmRleCBiY2RmZTkxLi5kZmIyM2FjIDEw MDY0NAotLS0gYS90ZXN0LXN1aXRlL3Rlc3RzL2hhc2gudGVzdAorKysgYi90ZXN0LXN1aXRlL3Rl c3RzL2hhc2gudGVzdApAQCAtMjkyLDMgKzI5MiwxOSBAQAogICAgZXhjZXB0aW9uOndyb25nLXR5 cGUtYXJnCiAgICAoaGFzaHgtc2V0ISAobGFtYmRhIChrIHMpIDEpIChsYW1iZGEgKGsgYWwpICN0 KSAobWFrZS1oYXNoLXRhYmxlKSAnZm9vICdiYXIpKQogICApCisKKworOzs7Cis7OzsgaGFzaC1j b3VudAorOzs7CisKKyh3aXRoLXRlc3QtcHJlZml4ICJoYXNoLWNvdW50IgorICAobGV0ICgodGFi bGUgKG1ha2UtaGFzaC10YWJsZSkpKQorICAgIChoYXNocS1zZXQhIHRhYmxlICdmb28gImJhciIp CisgICAgKGhhc2hxLXNldCEgdGFibGUgJ2JyYXogInpvbmsiKQorICAgIChoYXNocS1jcmVhdGUt aGFuZGxlISB0YWJsZSAnZnJvYiAjZikKKworICAgIChwYXNzLWlmIChlcXVhbD8gMyAoaGFzaC1j b3VudCAjZiB0YWJsZSkpKQorCisgICAgKHBhc3MtaWYgKGVxdWFsPyAyIChoYXNoLWNvdW50IChs YW1iZGEgKGsgdikKKyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoc3RyaW5n PyB2KSkgdGFibGUpKSkpKQotLSAKMS43LjEwLjQKCg== --14dae9340fe1f9aaae04d5ea575b--