hashtable-copy(3scm)

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

πŸ’… hashtable-copy β€” like, a whole manual page, bestie

okay so this is hashtable-copy from the Scheme Programmer's Manual (yes the 3scm one, you're in the right place πŸŽ€), and honestly? it's giving "tiny procedure, big slay."


✨ NAME ✨

hashtable-copy β€” copy a hashtable. that's it. that's the whole vibe. 🫢


πŸ“š LIBRARY πŸ“š

bestie you gotta import it first, no cap:

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

🌸 SYNOPSIS 🌸

the two forms, exactly as written, don't @ me:

(hashtable-copy hashtable)
(hashtable-copy hashtable mutable)

πŸ’– DESCRIPTION πŸ’–

so basically it returns a copy of hashtable. like a clone. a twin. a doppelgΓ€nger but make it functional. πŸ‘‘

now here's the tea πŸ“ β€” if the mutable argument is provided and it's true, then the returned hashtable is mutable. otherwise? it's immutable. locked. frozen. no takebacks. πŸ’€


🎁 RETURN VALUES 🎁

returns a single value: a hashtable. one (1) hashtable. that's the whole return. 🌈


🌟 EXAMPLES 🌟

okay this example is actually iconic, watch:

(define (alist->eqv-hashtable alist)
  (let ((ht (make-eqv-hashtable)))
    (do ((alist alist (cdr alist)))
        ((null? alist)
         (hashtable-copy ht))
      (hashtable-set! ht (caar alist) (cdar alist)))))

like, it builds up the hashtable then copies it on the way out. main character energy honestly.

and then:

(hashtable-entries
 (alist->eqv-hashtable '((0 . zero) (1 . one) (2 . three))))
    => #(0 1 2)
       #(zero one three)

see how the entries come back? chef's kiss. πŸ”₯ (yes, (2 . three) is in the original, it's not a typo, leave it alone 😭)


🧠 RATIONALE 🧠

why even allow immutability? because allowing the hashtable to be immutable lets implementations create an optimized representation for the hashtable. so it's not just cute, it's efficient. iykyk. ✨


πŸ¦‹ COMPATIBILITY πŸ¦‹

this procedure is unique to R6RS. so if you're on something else? not me finding out the hard way πŸ™ˆ


πŸ’€ 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 must be a hashtable. like, bestie, it has to actually BE a hashtable, okay?? 🫠

πŸ‘€ SEE ALSO πŸ‘€

  • make-hashtable(3scm) β€” go read that one too, it's the whole origin story 🧚

⭐ STANDARDS ⭐

R6RS. periodt. πŸ’…


πŸ•°οΈ HISTORY πŸ•°οΈ

this procedure first appeared in R6RS, but a similar procedure was also present in SRFI-69. so it's got lore. πŸ“–


πŸ‘‘ 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/. 🫢


and that's the page!! mwah πŸ’‹ β€” this was hashtable-copy(3scm), dated 2023-01-22, from the Scheme Programmer's Manual (that's the source/version right there, don't say i never told you). go copy some hashtables, bestie, you got this πŸ¦‹πŸ’œ

πŸ“¦ where this page comes from