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 (lambda (x) (+ x 65)) #(0 1 2)))
When I successfully run stak main.scm
Then the stdout should contain exactly “ABC”.
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 |