bytevector-u8-set!(3scm)

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

πŸ’– bytevector-u8-set!(3scm) πŸ’–

Okay bestie, so we're looking at bytevector-u8-set!(3scm) from the Scheme Programmer's Manual, and it's literally the glow-up your bytevectors have been waiting for. This is the "modify a byte in a bytevector" era and we are obsessed. ✨


πŸŽ€ LIBRARY πŸŽ€

So first things first, you gotta import this vibe from the right place. Like, pick your era:

(import (rnrs))                     ;R6RS
(import (rnrs bytevectors))         ;R6RS
(import (scheme base))              ;R7RS

No cap, these are the three possible places to get this procedure from. Slay.


πŸ¦‹ SYNOPSIS πŸ¦‹

Okay so the way you call this iconic procedure is:

(bytevector-u8-set! bytevector k octet)
(bytevector-s8-set! bytevector k byte)  ;R6RS

That's it. That's the whole mood. πŸ’…


🌸 DESCRIPTION 🌸

So here's the tea, bestie: bytevector-u8-set! is gonna take your octet and store it at index k of your bytevector. It's giving "main character energy" because it's literally changing the bytevector in place. Periodt. πŸ”₯

And bytevector-s8-set!? That one stores the two's complement representation of byte. Not me explaining two's complement in a manpage, but here we are and it ate. 😭


⭐ RETURN VALUES ⭐

Returns unspecified values. Yep, that's it. It doesn't return anything useful, it just does the thing and dips. Lowkey iconic behavior. πŸ’€


🌈 EXAMPLES 🌈

Okay so here's a real example that's literally gonna make you understand everything:

(let ((b (make-bytevector 16 -127)))
  (bytevector-s8-set! b 0 -126)
  (bytevector-u8-set! b 1 246)

  (list
    (bytevector-s8-ref b 0)
    (bytevector-u8-ref b 0)
    (bytevector-s8-ref b 1)
    (bytevector-u8-ref b 1)))
              => (-126 130 -10 246)

Look at that output bestie: (-126 130 -10 246). It's giving "the math is mathing" and we love that for us. 🫢


πŸ‘‘ COMPATIBILITY πŸ‘‘

Okay so here's the thing: bytevector-s8-set! is literally absent from R7RS. Like, it just didn't make the cut. But don't worry bestie, because there's a possible implementation shown below and it's giving "I made it work":

(define (bytevector-s8-set! bytevector k byte)
  (let ((octet (if (< byte 0) (+ 256 byte) byte)))
    (bytevector-u8-set! bytevector k octet)))

See what they did there? They converted the signed byte to an unsigned octet and then used the big sister bytevector-u8-set! to handle it. Highkey clever. πŸ’–


πŸ’€ ERRORS πŸ’€

This procedure can raise exceptions with the following condition types, and you need to know about them:

  • &assertion (R6RS) β€” The wrong number of arguments was passed or an argument was outside its domain. The index k was not a valid index for bytevector. Basically if you mess up the vibe check, this is what you get. So make sure your k is actually valid, okay? πŸ™ˆ

πŸ“ SEE ALSO πŸ“

  • bytevector-u16-set!(3scm) β€” because sometimes you need more than 8 bits and that's valid.

🧚 STANDARDS 🧚

R6RS. That's the standard, bestie. It's the era this procedure was born in. πŸ’œ


πŸ’œ 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/. I know, I know, it's not a link you can click but it's still iconic. πŸŽ€


Alright bestie, that's the whole page. This manual page is from the Scheme Programmer's Manual and it's dated 2021-02-28. Stay slaying with those bytevectors and remember: no cap, you're now a bytevector-u8-set! expert. πŸ’…βœ¨

πŸ“¦ where this page comes from