[cdr-discuss] notes on CDR 2 - generic hash table

Chris Dean ctdean at sokitomi.com
Thu Nov 23 04:41:40 UTC 2006


Gary King <gwking at metabang.com> writes:
> 2. Naming. I like longer names (they are easier to read and
>    completion makes them easier to type). 

Good suggestions.  The Scheme SRFI documents have some nice names for
hash table functions:

http://srfi.schemers.org/srfi-69/srfi-69.html
http://srfi.schemers.org/srfi-90/srfi-90.html

> hash-lookup

I personally like REF or GET here, fwiw.

As one data point in this discussion I'll describe what I do in my own
code: I have a thin wrapper around the LispWorks hash table code that
uses the names and behavior I want.  I chose DICT so I wouldn't be
confused by the CL functions.

    make-dict
    dict
    dict?
    dict-get
    dict-delete!
    dict-exists?
    dict-clear!
    dict-count
    map-dict
    for-each-dict
    dict->keys
    dict->values
    alist->dict
    dict->alist
    plist->dict
    dict->plist
    dict-emtpy?
    dict-best

(There are a few functions missing in this list.)

MAKE-DICT allows you to specify the test function and the hashing
function as well as a flag for a weak hash.  My default test and
hashing functions are my own generic functions OBJECT-HASH and
OBJECT-EQUAL? that try to do the "right thing" for a key of any type.
The general case is obviously slower than a simple EQL hash table.

I also use the Iterate package and have IN-DICT and COLLECT-DICT
forms.

One small point additional point.  My map-dict will cons up the
results in a list (it's more like mapcar than mapc) and the
for-each-dict just iterates over the function and doesn't do any
consing.

Cheers,
Chris Dean



More information about the cdr-discuss mailing list