hashtable-update! - update an entry in a hashtable

LIBRARY

(import (rnrs))                     ;R6RS
(import (rnrs hashtables))          ;R6RS

SYNOPSIS

(hashtable-update! hashtable key proc default)

DESCRIPTION

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.

IMPLEMENTATION NOTES

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)))

RETURN VALUES

This procedure returns unspecified values.

EXAMPLES

;; 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))

APPLICATION USAGE

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.

COMPATIBILITY

This procedure is new to R6RS but compatible variants can be found in SRFI-69, SRFI-125 and SRFI-126.

ERRORS

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.

SEE ALSO

hashtable-set!(3scm)

STANDARDS

R6RS

HISTORY

This procedure first appeared in R6RS and a similar procedure can be found in SRFI-69, which predates R6RS.

AUTHORS

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.


Markup created by unroff 1.0sc,    March 04, 2023.