hashtable-update!(3scm)
π
hashtable-update!(3scm) β welcome to the Scheme Programmer's Manual, bestie π
okay so this page is all about one iconic little procedure from the Scheme Programmer's Manual: hashtable-update!. It updates an entry in a hashtable, and it is very useful. Let's get into it. π
β¨ NAME β¨
hashtable-update! β update an entry in a hashtable. That's the whole vibe. π
π LIBRARY π
Import it like this, bestie:
(import (rnrs)) ;R6RS
(import (rnrs hashtables)) ;R6RS
β¨ SYNOPSIS β¨
(hashtable-update! hashtable key proc default)
π DESCRIPTION π
Here's the tea: hashtable-update! applies proc to the value in hashtable that's associated with key. If hashtable does not contain an association for key, it applies proc to default instead. Then the hashtable is changed so that key is associated with the value returned by proc. slay. π¦
π§ IMPLEMENTATION NOTES π§
Behavior-wise, hashtable-update! is equivalent to this code:
(hashtable-set!
hashtable key
(proc (hashtable-ref
hashtable key default)))
But it may be implemented more efficiently in cases where the implementation can avoid multiple lookups of the same key. cute, efficient, iconic. β¨
πΈ RETURN VALUES πΈ
This procedure returns unspecified values. That's it. No extra return-value moment. π
π EXAMPLES π
This R6RS example places integers in one of five buckets by using hashtable-update!. Observe:
;; 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, like in the example above. There are also other usage patterns, such as counting occurrences of an element. very useful, very main character. π
π COMPATIBILITY π
This procedure is new to R6RS, but compatible variants can be found in SRFI-69, SRFI-125 and SRFI-126. iykyk. π«Ά
π 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,hashtableshould be a hashtable,keyanddefaultshould be in the domain ofhashtable's hash and equal procedures,procshould be a procedure that accepts one arguments, returns one value and does not mutatehashtable.
bestie, please note:procshould not mutatehashtable. That's the rule. π¨
π SEE ALSO π
hashtable-set!(3scm)β go read that one too, bestie.
β 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/.
Source/version check: this is hashtable-update!(3scm) from the Scheme Programmer's Manual, dated 2023-01-21. Byeee, go update your hashtables responsibly πβ¨
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms