[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Hash table read syntax in guile 2.0
From: |
Daniel Hartwig |
Subject: |
Re: Hash table read syntax in guile 2.0 |
Date: |
Sun, 17 Feb 2013 19:59:41 +0800 |
On 17 February 2013 11:55, Hengqing Hu <address@hidden> wrote:
> Dear list,
>
> It seems the read syntax of a hash table has been silently changed.
>
> Now (make-hash-table) produces
> #<hash-table 172f1c0 0/31>
> instead what was in 1.8
> #<hash-table 0/31>
>
> 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 ‘size’ and ‘length’ 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 “number of
elements” 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, ‘size’ 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.
0001-add-hash-count-for-native-tables.patch
Description: Binary data
- Hash table read syntax in guile 2.0, Hengqing Hu, 2013/02/16
- Re: Hash table read syntax in guile 2.0,
Daniel Hartwig <=
- Re: Hash table read syntax in guile 2.0, Hengqing Hu, 2013/02/17
- Re: Hash table read syntax in guile 2.0, Daniel Hartwig, 2013/02/17
- Re: Hash table read syntax in guile 2.0, Hengqing Hu, 2013/02/17
- Re: Hash table read syntax in guile 2.0, Daniel Hartwig, 2013/02/17
- Re: Hash table read syntax in guile 2.0, Hengqing Hu, 2013/02/17
- Re: Hash table read syntax in guile 2.0, Daniel Hartwig, 2013/02/17
- Re: Hash table read syntax in guile 2.0, Hengqing Hu, 2013/02/17
- Re: Hash table read syntax in guile 2.0, Daniel Hartwig, 2013/02/18
- Re: Hash table read syntax in guile 2.0, Hengqing Hu, 2013/02/18
- Re: Hash table read syntax in guile 2.0, Daniel Hartwig, 2013/02/18