3.11 Vectors
Vectors in Guide: Racket introduces vectors.
A vector is a fixed-length array with constant-time access and update of the vector slots, which are numbered from 0 to one less than the number of slots in the vector.
Two vectors are equal? if they have the same length, and if the values in corresponding slots of the vectors are equal?.
A vector can be mutable or immutable. When an immutable vector is provided to a procedure like vector-set!, the exn:fail:contract exception is raised. Vectors generated by the default reader (see Reading Strings) are immutable.
A vector can be used as a single-valued sequence (see Sequences). The elements of the vector serve as elements of the sequence. See also in-vector.
(make-vector size [v]) → vector? |
size : exact-nonnegative-integer? |
v : any/c = 0 |
| |||||||
v : any/c |
(vector-length vec) → exact-nonnegative-integer? |
vec : vector? |
(vector-ref vec pos) → any/c |
vec : vector? |
pos : exact-nonnegative-integer? |
(vector-set! vec pos v) → void? |
vec : (and/c vector? (not/c immutable?)) |
pos : exact-nonnegative-integer? |
v : any/c |
(vector->list vec) → list? |
vec : vector? |
(list->vector lst) → vector? |
lst : list? |
(vector->immutable-vector vec) → (and/c vector? immutable?) |
vec : vector? |
(vector-fill! vec v) → void? |
vec : (and/c vector? (not/c immutable?)) |
v : any/c |
| |||||||||||||||||||||||||||||||||||
dest : (and/c vector? (not/c immutable?)) | |||||||||||||||||||||||||||||||||||
dest-start : exact-nonnegative-integer? | |||||||||||||||||||||||||||||||||||
src : vector? | |||||||||||||||||||||||||||||||||||
src-start : exact-nonnegative-integer? = 0 | |||||||||||||||||||||||||||||||||||
src-end : exact-nonnegative-integer? = (vector-length src) |
Examples: |
> (define v (vector 'A 'p 'p 'l 'e)) |
> (vector-copy! v 4 #(y)) |
> (vector-copy! v 0 v 3 4) |
> v |
'#(l p p l y) |
(vector->values vec [start-pos end-pos]) → any |
vec : vector? |
start-pos : exact-nonnegative-integer? = 0 |
end-pos : exact-nonnegative-integer? = (vector-length vec) |
(build-vector n proc) → vector? |
n : exact-nonnegative-integer? |
proc : (exact-nonnegative-integer? . -> . any/c) |
Example: |
> (build-vector 5 add1) |
'#(1 2 3 4 5) |
3.11.1 Additional Vector Functions
(vector-set*! vec pos v ... ...) → void? |
vec : (and/c vector? (not/c immutable?)) |
pos : exact-nonnegative-integer? |
v : any/c |
(vector-map proc vec ...+) → vector? |
proc : procedure? |
vec : vector? |
Example: |
> (vector-map + #(1 2) #(3 4)) |
'#(4 6) |
(vector-map! proc vec ...+) → vector? |
proc : procedure? |
vec : vector? |
Examples: |
> (define v #(1 2 3 4)) |
> (vector-map! add1 v) |
'#(2 3 4 5) |
> v |
'#(2 3 4 5) |
(vector-append vec ...) → vector? |
vec : vector? |
Example: |
> (vector-append #(1 2) #(3 4)) |
'#(1 2 3 4) |
(vector-take vec pos) → vector? |
vec : vector? |
pos : exact-nonnegative-integer? |
Example: |
> (vector-take #(1 2 3 4) 2) |
'#(1 2) |
(vector-take-right vec pos) → vector? |
vec : vector? |
pos : exact-nonnegative-integer? |
Example: |
> (vector-take-right #(1 2 3 4) 2) |
'#(3 4) |
(vector-drop vec pos) → vector? |
vec : vector? |
pos : exact-nonnegative-integer? |
Example: |
> (vector-drop #(1 2 3 4) 2) |
'#(3 4) |
(vector-drop-right vec pos) → vector? |
vec : vector? |
pos : exact-nonnegative-integer? |
Example: |
> (vector-drop-right #(1 2 3 4) 2) |
'#(1 2) |
| ||||||||
vec : vector? | ||||||||
pos : exact-nonnegative-integer? |
(values (vector-take vec pos) (vector-drop vec pos))
except that it can be faster.
Example: |
> (vector-split-at #(1 2 3 4 5) 2) |
'#(1 2) |
'#(3 4 5) |
| ||||||||
vec : vector? | ||||||||
pos : exact-nonnegative-integer? |
(values (vector-take-right vec pos) (vector-drop-right vec pos))
except that it can be faster.
Example: |
> (vector-split-at-right #(1 2 3 4 5) 2) |
'#(1 2 3) |
'#(4 5) |
(vector-copy vec [start end]) → vector? |
vec : vector? |
start : exact-nonnegative-integer? = 0 |
end : exact-nonnegative-integer? = (vector-length v) |
Examples: |
> (vector-copy #(1 2 3 4)) |
'#(1 2 3 4) |
> (vector-copy #(1 2 3 4) 3) |
'#(4) |
> (vector-copy #(1 2 3 4) 2 3) |
'#(3) |
(vector-filter pred vec) → vector? |
pred : procedure? |
vec : vector? |
Example: |
> (vector-filter even? #(1 2 3 4 5 6)) |
'#(2 4 6) |
(vector-filter-not pred vec) → vector? |
pred : procedure? |
vec : vector? |
Example: |
> (vector-filter-not even? #(1 2 3 4 5 6)) |
'#(1 3 5) |
(vector-count proc vec ...+) → exact-nonnegative-integer? |
proc : procedure? |
vec : vector? |
Examples: |
> (vector-count even? #(1 2 3 4 5)) |
2 |
> (vector-count = #(1 2 3 4 5) #(5 4 3 2 1)) |
1 |
Examples: |
> (vector-argmin car #((3 pears) (1 banana) (2 apples))) |
'(1 banana) |
> (vector-argmin car #((1 banana) (1 orange))) |
'(1 banana) |
Examples: |
> (vector-argmax car #((3 pears) (1 banana) (2 apples))) |
'(3 pears) |
> (vector-argmax car #((3 pears) (3 oranges))) |
'(3 pears) |
(vector-member v lst) → (or/c natural-number/c #f) |
v : any/c |
lst : vector? |
Examples: |
> (vector-member 2 (vector 1 2 3 4)) |
1 |
> (vector-member 9 (vector 1 2 3 4)) |
#f |
(vector-memv v vec) → (or/c natural-number/c #f) |
v : any/c |
vec : vector? |
Examples: |
> (vector-memv 2 (vector 1 2 3 4)) |
1 |
> (vector-memv 9 (vector 1 2 3 4)) |
#f |
(vector-memq v vec) → (or/c natural-number/c #f) |
v : any/c |
vec : vector? |
Examples: |
> (vector-memq 2 (vector 1 2 3 4)) |
1 |
> (vector-memq 9 (vector 1 2 3 4)) |
#f |