Version: 5.0.1
37 Hash Tables
(require unstable/hash) |
This library is unstable;
compatibility will not be maintained.
See Unstable for more information.
This module provides tools for manipulating hash tables.
37.1 Hash Table Lookup
(hash-ref/check h k) → any/c |
h : hash? |
k : (lambda (k) (hash-has-key? h k)) |
Looks up key k in hash table h. Raises a contract error if
h has no entry for k. Equivalent to (hash-ref h k),
except for the specific exception value raised.
Example: |
> (hash-ref/check (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2) |
'two |
(hash-ref/identity h k) → any/c |
h : hash? |
k : any/c |
Looks up key k in hash table h. Returns k if
h has no entry for k. Equivalent to
(hash-ref h k (lambda () k)).
Examples: |
> (hash-ref/identity (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2) |
'two |
> (hash-ref/identity (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4) |
4 |
(hash-ref/default h k v) → any/c |
h : hash? |
k : any/c |
v : any/c |
Looks up key k in hash table h. Returns v if
h has no entry for k. Equivalent to
(hash-ref h k (lambda () v)).
Examples: |
> (hash-ref/default (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2 'other) |
'two |
> (hash-ref/default (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4 'other) |
'other |
Looks up key k in hash table h. Returns the result of
applying f (in tail position) if h has no entry for
k. Equivalent to (hash-ref h k f).
Examples: |
> (hash-ref/failure (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2 gensym) |
'two |
> (hash-ref/failure (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4 gensym) |
'g49541 |
37.2 Hash Table Accessors
(hash-equal? h) → boolean? |
h : hash? |
Reports whether h maps keys according to equal?.
Examples: |
> (hash-equal? #hash()) |
#t |
> (hash-equal? #hasheq()) |
#f |
> (hash-equal? #hasheqv()) |
#f |
(hash-has-key? h k) → boolean? |
h : hash? |
k : any/c |
Reports whether h has an entry for k. This function is
re-exported from scheme/base. In versions of Racket before
hash-has-key? was implemented, this module provides its own definition.
Examples: |
> (hash-has-key? (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2) |
#t |
> (hash-has-key? (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4) |
#f |
(hash-domain h) → list? |
h : hash? |
Produces the domain of a hash table as a list of keys.
Example: |
> (hash-domain (make-immutable-hash '([1 . one] [2 . two] [3 . three]))) |
'(1 2 3) |
(hash-range h) → list? |
h : hash? |
Produces the range of a hash table as a list of values.
Example: |
> (hash-range (make-immutable-hash '([1 . one] [2 . two] [3 . three]))) |
'(one two three) |
37.3 Hash Table Combinations
| ||||||||||||||||
→ (and/c hash? hash-can-functional-set?) | ||||||||||||||||
h0 : (and/c hash? hash-can-functional-set?) | ||||||||||||||||
h : hash? | ||||||||||||||||
| ||||||||||||||||
|
Computes the union of h0 with each hash table h by functional
update, adding each element of each h to h0 in turn. For each
key k and value v, if a mapping from k to some value
v0 already exists, it is replaced with a mapping from k to
(combine/key k v0 v).
Examples: | |||
> (hash-union (make-immutable-hash '([1 . one])) (make-immutable-hash '([2 . two])) (make-immutable-hash '([3 . three]))) | |||
'#hash((1 . one) (2 . two) (3 . three)) | |||
| |||
'#hash((1 . (one uno ein une)) (2 . (two dos zwei deux))) |
| ||||||||||||||||||||||||||||
h0 : (and/c hash? hash-mutable?) | ||||||||||||||||||||||||||||
h : hash? | ||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||
|
Computes the union of h0 with each hash table h by mutable
update, adding each element of each h to h0 in turn. For each
key k and value v, if a mapping from k to some value
v0 already exists, it is replaced with a mapping from k to
(combine/key k v0 v).
Examples: | |||
| |||
> h | |||
'#hash() | |||
> (hash-union! h (make-immutable-hash '([1 one uno] [2 two dos]))) | |||
> h | |||
'#hash((1 . (one uno)) (2 . (two dos))) | |||
| |||
> h | |||
'#hash((1 . (one uno ein une)) (2 . (two dos zwei deux))) |