hashtable-update!(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’… 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, 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.
bestie, please note: proc should not mutate hashtable. 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