Version: 5.1
36 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.
36.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) |
'g1993 |
36.2 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))) |