Bytevector
Write a bytevector
Section titled “Write a bytevector”Given a file named “main.scm” with:
(import (scheme base))
(write-bytevector #u8(65 66 67))When I successfully run stak main.scm
Then the stdout should contain exactly “ABC”.
Get a length of a bytevector
Section titled “Get a length of a bytevector”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (bytevector-length <value>) <length>) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| value | length |
|---|---|
| #u8() | 0 |
| #u8(0) | 1 |
| #u8(0 0) | 2 |
| #u8(0 0 0) | 3 |
Reference a value in a bytevector
Section titled “Reference a value in a bytevector”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (bytevector-u8-ref <vector> <index>))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| vector | index |
|---|---|
| #u8(65) | 0 |
| #u8(65 66) | 0 |
| #u8(66 65) | 1 |
| #u8(65 66 66) | 0 |
| #u8(66 65 66) | 1 |
| #u8(66 66 65) | 2 |
Set a value in a bytevector
Section titled “Set a value in a bytevector”Given a file named “main.scm” with:
(import (scheme base))
(define xs (bytevector <values>))
(bytevector-u8-set! xs <index> <value>)
(write-u8 (if (equal? xs #u8(<result>)) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| values | index | value | result |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 1 | 0 | 2 | 2 1 |
| 0 1 | 1 | 2 | 0 2 |
| 0 1 2 | 0 | 3 | 3 1 2 |
| 0 1 2 | 1 | 3 | 0 3 2 |
| 0 1 2 | 2 | 3 | 0 1 3 |
Append bytevectors
Section titled “Append bytevectors”Given a file named “main.scm” with:
(import (scheme base) (srfi 1))
(define xs (bytevector-append <values>))
(for-each (lambda (index) (write-u8 (bytevector-u8-ref xs index))) (iota (bytevector-length xs)))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| values | output |
|---|---|
| #u8() | |
| #u8() #u8() | |
| #u8(65) | A |
| #u8(65) #u8(66) | AB |
| #u8(65) #u8(66) #u8(67) | ABC |
| #u8(65) #u8(66 67) #u8(68 69 70) | ABCDEF |
Copy a bytevector
Section titled “Copy a bytevector”Given a file named “main.scm” with:
(import (scheme base) (srfi 1))
(define xs (bytevector-copy <value>))
(for-each (lambda (index) (write-u8 (bytevector-u8-ref xs index))) (iota (bytevector-length xs)))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| value | output |
|---|---|
| #u8() | |
| #u8(65) | A |
| #u8(65 66) | AB |
| #u8(65 66 67) | ABC |
Copy a bytevector in place
Section titled “Copy a bytevector in place”Given a file named “main.scm” with:
(import (scheme base) (srfi 1))
(define xs (bytevector <values>))
(bytevector-copy! xs <arguments>)
(for-each (lambda (index) (write-u8 (+ 65 (bytevector-u8-ref xs index)))) (iota (bytevector-length xs)))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| values | arguments | output |
|---|---|---|
| 0 #u8() | ||
| 0 1 2 | 0 #u8(3 4 5) | DEF |
| 0 1 2 | 1 #u8(3 4) | ADE |
| 0 1 2 | 2 #u8(3) | ABD |
| 0 1 2 3 4 | 1 #u8(5 6 7) | AFGHE |
| 0 1 2 3 | 1 #u8(4 5 6 7) 1 | AFGH |
| 0 1 2 3 | 1 #u8(4 5 6 7) 1 3 | AFGD |
Large bytevector
Section titled “Large bytevector”Reference an element
Section titled “Reference an element”Given a file named “main.scm” with:
(import (scheme base) (scheme write))
(write (bytevector-u8-ref (make-bytevector <length> 42) <index>))When I successfully run stak main.scm
Then the stdout should contain exactly “42”.
Examples
Section titled “Examples”| length | index |
|---|---|
| 1 | 0 |
| 2 | 0 |
| 2 | 1 |
| 3 | 0 |
| 3 | 1 |
| 3 | 2 |
| 8 | 0 |
| 8 | 1 |
| 8 | 6 |
| 8 | 7 |
| 9 | 0 |
| 9 | 1 |
| 9 | 7 |
| 9 | 8 |
| 64 | 63 |
| 65 | 64 |
| 512 | 511 |
| 513 | 512 |
| 4096 | 4095 |
| 4097 | 4096 |
Use a bytevector literal
Section titled “Use a bytevector literal”Given a file named “main.scm” with:
(import (scheme base) (scheme write))
(define xs (include "./value.scm"))
(write (bytevector-u8-ref xs <index>))And a file named “write.scm” with:
(import (scheme base) (scheme write) (srfi 1))
(write (apply bytevector (map (lambda (x) (remainder x 256)) (iota <length>))))And I run the following script:
stak write.scm > value.scmWhen I successfully run stak main.scm
Then the stdout should contain exactly “<index>”.
Examples
Section titled “Examples”| length | index |
|---|---|
| 1 | 0 |
| 2 | 0 |
| 2 | 1 |
| 512 | 0 |
| 512 | 1 |
| 512 | 254 |
| 512 | 255 |
| 4096 | 0 |
| 4096 | 1 |
| 4096 | 254 |
| 4096 | 255 |
| 8192 | 0 |
| 8192 | 1 |
| 8192 | 254 |
| 8192 | 255 |