Vector
Make a vector
Section titled “Make a vector”Given a file named “main.scm” with:
(import (scheme base))
(make-vector 42)When I successfully run stak main.scm
Then the exit status should be 0.
Make a vector with a fill value
Section titled “Make a vector with a fill value”Given a file named “main.scm” with:
(import (scheme base))
(make-vector 42 #t)When I successfully run stak main.scm
Then the exit status should be 0.
Convert a vector to a list
Section titled “Convert a vector to a list”Given a file named “main.scm” with:
(import (scheme base))
(vector-for-each write-u8 #(65 66 67))When I successfully run stak main.scm
Then the stdout should contain exactly “ABC”.
Get a length of a vector
Section titled “Get a length of a vector”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (= (vector-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 |
|---|---|
| #() | 0 |
| #(1) | 1 |
| #(1 2) | 2 |
| #(1 2 3) | 3 |
| (make-vector 3) | 3 |
| (make-vector 3 #t) | 3 |
| (vector 1 2 3) | 3 |
Get an element in a vector
Section titled “Get an element in a vector”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (vector-ref <vector> <index>))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| vector | index | output |
|---|---|---|
| (vector 65) | 0 | A |
| (vector 65 66) | 0 | A |
| (vector 65 66) | 1 | B |
| (vector 65 66 67) | 0 | A |
| (vector 65 66 67) | 1 | B |
| (vector 65 66 67) | 2 | C |
Set an element in a vector
Section titled “Set an element in a vector”Given a file named “main.scm” with:
(import (scheme base))
(define xs <vector>)
(vector-set! xs <index> 88)
(vector-for-each write-u8 xs)When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| vector | index | output |
|---|---|---|
| (vector 65) | 0 | X |
| (vector 65 66) | 0 | XB |
| (vector 65 66) | 1 | AX |
| (vector 65 66 67) | 0 | XBC |
| (vector 65 66 67) | 1 | AXC |
| (vector 65 66 67) | 2 | ABX |
Append vectors
Section titled “Append vectors”Given a file named “main.scm” with:
(import (scheme base))
(vector-for-each write-u8 (vector-append <values>))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| values | output |
|---|---|
| #() | |
| #() #() | |
| #(65) | A |
| #(65) #(66) | AB |
| #(65) #(66) #(67) | ABC |
| #(65) #(66 67) #(68 69 70) | ABCDEF |
Map a function on a vector
Section titled “Map a function on a vector”Given a file named “main.scm” with:
(import (scheme base))
(vector-for-each write-u8 (vector-map + <vectors>))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| vectors | output |
|---|---|
| #(65 66 67) | ABC |
| #(65 66 67) #(0 1 2) | ACE |
| #(65 66 67) #(0 1 2) #(3 4) | DG |
Iterate over a vector
Section titled “Iterate over a vector”Given a file named “main.scm” with:
(import (scheme base))
(vector-for-each (lambda xs (write-u8 (apply + xs))) <vectors>)When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| vectors | output |
|---|---|
| #() | |
| #(65) | A |
| #(65 66) | AB |
| #(65 66 67) | ABC |
| #(65 66 67) #(0 1 2) | ACE |
| #(65 66 67) #(0 1 2) #(3 4) | DG |
Copy a vector
Section titled “Copy a vector”Given a file named “main.scm” with:
(import (scheme base))
(vector-for-each write-u8 (vector-copy <value>))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| value | output |
|---|---|
| #() | |
| #(65) | A |
| #(65 66) | AB |
| #(65 66 67) | ABC |
Copy a vector in place
Section titled “Copy a vector in place”Given a file named “main.scm” with:
(import (scheme base))
(define xs (vector <values>))
(vector-copy! xs <arguments>)
(for-each (lambda (x) (write-u8 (+ x 65))) (vector->list xs))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| values | arguments | output |
|---|---|---|
| 0 | 0 #() | A |
| 0 1 2 | 0 #(3 4 5) | DEF |
| 0 1 2 | 1 #(3 4) | ADE |
| 0 1 2 | 2 #(3) | ABD |
| 0 1 2 3 4 | 1 #(5 6 7) | AFGHE |
| 0 1 2 3 | 1 #(4 5 6 7) 1 | AFGH |
| 0 1 2 3 | 1 #(4 5 6 7) 1 3 | AFGD |
Fill a vector
Section titled “Fill a vector”Given a file named “main.scm” with:
(import (scheme base))
(define xs (vector <values>))
(vector-fill! xs <arguments>)
(for-each (lambda (x) (write-u8 (+ x 65))) (vector->list xs))When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”| values | arguments | output |
|---|---|---|
| 0 | 1 | B |
| 0 1 | 2 0 | CC |
| 0 1 | 2 1 | AC |
| 0 1 2 | 3 | DDD |
| 0 1 2 3 | 4 1 3 | AEED |
Large vector
Section titled “Large vector”Reference an element
Section titled “Reference an element”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (vector-ref (make-vector <length> 65) <index>))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
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 |
Convert values between a list and a vector
Section titled “Convert values between a list and a vector”Given a file named “main.scm” with:
(import (scheme base) (srfi 1))
(define xs (iota <count>))
(write-u8 (if (equal? (vector->list (list->vector xs)) xs) 65 66))When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”| count |
|---|
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 16 |
| 17 |
| 32 |
| 33 |
| 64 |
| 65 |
| 128 |
| 129 |
| 512 |
| 513 |
| 4096 |
| 4097 |
Use a vector literal
Section titled “Use a vector literal”Given a file named “main.scm” with:
(import (scheme base))
(define xs (include "./value.scm"))
(write-u8 (if (= (vector-ref xs <index>) <index>) 65 66))And a file named “write.scm” with:
(import (scheme base) (scheme write) (srfi 1))
(write (list->vector (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 “A”.
Examples
Section titled “Examples”| length | index |
|---|---|
| 1 | 0 |
| 2 | 0 |
| 2 | 1 |
| 512 | 0 |
| 512 | 1 |
| 512 | 510 |
| 512 | 511 |
| 4096 | 0 |
| 4096 | 1 |
| 4096 | 4094 |
| 4096 | 4095 |
| 8192 | 0 |
| 8192 | 1 |
| 8192 | 8190 |
| 8192 | 8191 |