hashtable-update! - update an entry in a hashtable
(import (rnrs)) ;R6RS
(import (rnrs hashtables)) ;R6RS
(hashtable-update! hashtable key proc default)
Applies proc to the value in hashtable associated with key,
or to default if hashtable does not contain an association for
key. The hashtable is then changed to associate key with
the value returned by proc.
The behavior of hashtable-update! is equivalent to the following code,
but may be implemented more efficiently in cases where the implementation can
avoid multiple lookups of the same key:
(hashtable-set!
hashtable key
(proc (hashtable-ref
hashtable key default)))
This procedure returns unspecified values.
;; This R6RS example places integers in one of
;; five buckets by using hashtable-update!.
(import (rnrs))
(do ((ht (make-eqv-hashtable))
(i 0 (+ i 1)))
((= i 42)
(hashtable-entries ht))
(hashtable-update! ht (mod i 5)
(lambda (lst)
(cons i lst))
'()))
=> #(0 1 2 3 4)
#((40 35 30 25 20 15 10 5 0)
(41 36 31 26 21 16 11 6 1)
(37 32 27 22 17 12 7 2)
(38 33 28 23 18 13 8 3)
(39 34 29 24 19 14 9 4))
This procedure is commonly used to accumulate values in a hashtable, as in the
example above. There are also other usage patterns such as counting
occurrences of an element.
This procedure is new to R6RS but compatible variants can be found in SRFI-69,
SRFI-125 and SRFI-126.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
- The wrong number of arguments was passed or an argument was outside its
domain. In particular, hashtable should be a hashtable, key
and default should be in the domain of hashtable's hash and
equal procedures, proc should be a procedure that accepts one
arguments, returns one value and does not mutate hashtable.
This procedure first appeared in R6RS and a similar procedure can be found in
SRFI-69, which predates R6RS.