The (scheme vector) library provides constructors, predicates, iteration, searching, mutation, and conversion procedures for vectors.
The arguments vector, to, and from must be vectors. The argument list must be a list. The arguments k, length, at, start, and end must be exact non-negative integers.
A range begins at start (inclusive) and ends at end (exclusive). These arguments must satisfy (<= 0 start end (vector-length vector)) for the vector whose elements are selected. When omitted, start defaults to zero and end defaults to the length of that vector. In copying procedures, the range refers to from.
The argument f must be a procedure returning one value, except where multiple return values are specified. The argument pred? must be a procedure returning one value, which is treated as a boolean. When parallel elements of several vectors are passed to a procedure, they are passed in the order of the vector arguments.
The arguments obj, initial-seed, knil, and value may be arbitrary Scheme objects.
It is an error for the number of seeds to vary between iterations.
The fundamental vector constructor. Creates a vector whose length is length and iterates across each index k from zero (inclusive) to length (exclusive), applying f at each iteration to the current index and current seeds, in that order. The first value returned is the element to put in the kth slot of the new vector; the remaining values are the new seeds for the next iteration, one for each initial seed.
Note: The termination condition differs from that of unfold.
(vector-unfold (lambda (k obj) (values obj (- obj 1))) 10 0)⇒#(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
Construct a vector of consecutive non-negative integers.
(vector-unfold values 5)⇒#(0 1 2 3 4)
Copy vector.
(vector-unfold (lambda (k) (vector-ref vector k))
(vector-length vector))
Like vector-unfold, but uses f to generate elements from right to left rather than left to right. The first index used is (- length 1).
Note: The termination condition differs from that of unfold-right.
Construct a vector of pairs of non-negative integers whose values sum to 4.
(vector-unfold-right (lambda (k obj) (values (cons k obj) (+ obj 1))) 5 0)⇒#((0 . 4) (1 . 3) (2 . 2) (3 . 1) (4 . 0))
Reverse vector.
(vector-unfold-right
(lambda (k obj)
(values (vector-ref vector obj) (+ obj 1)))
(vector-length vector)
0)
Like vector-copy, but copies the elements of vector in reverse order.
(vector-reverse-copy '#(5 4 3 2 1 0) 1 5)⇒#(1 2 3 4)
list-of-vectors must be a list of vectors.
Appends each vector in list-of-vectors. This is equivalent to:
(apply vector-append list-of-vectors)
However, it may be implemented more efficiently than this call to apply and vector-append.
(vector-concatenate '(#(a b) #(c d)))⇒#(a b c d)
Returns a vector that contains every element of each vector from start (inclusive) to end (exclusive), in the specified order. This procedure is a generalization of vector-append.
(vector-append-subvectors '#(a b c d e) 0 2 '#(f g h i j) 2 4)⇒#(a b h i)
Returns #t if vector is empty, i.e. its length is zero, and #f otherwise.
(vector-empty? '#(a))⇒#f
(vector-empty? '#(()))⇒#f
(vector-empty? '#(#()))⇒#f
(vector-empty? '#())⇒#t
elt=? must be a procedure accepting two arguments and returning one value, which is treated as a boolean.
Vector structure comparator, generalized across user-specified element comparators. Two vectors are considered equal by vector= if and only if their lengths are the same and elt=? returns a true value for each pair of corresponding elements. elt=? is always applied to two arguments.
If there are zero or one vector arguments, #t is returned. The dynamic order in which comparisons of elements and of vectors are performed is unspecified.
(vector= eq? '#(a b c d) '#(a b c d))⇒#t
(vector= eq? '#(a b c d) '#(a b d c))⇒#f
(vector= = '#(1 2 3 4 5) '#(1 2 3 4))⇒#f
(vector= = '#(1 2 3 4) '#(1 2 3 4))⇒#t
The two trivial cases:
(vector= eq?)⇒#t
(vector= eq? '#(a))⇒#t
The following examples allocate vectors because it is unspecified whether literal vectors with the same external representation are eq?.
(vector= eq? (vector (vector 'a)) (vector (vector 'a)))⇒#f
(vector= equal? (vector (vector 'a)) (vector (vector 'a)))⇒#t
kons must accept one more argument than there are vectors and return one value.
The fundamental vector iterator. kons is iterated over each value in all of the vectors, stopping at the end of the shortest. It is applied as:
(kons state (vector-ref vector1 k) (vector-ref vector2 k) ...)
Here state is the current state value, which begins with knil and becomes whatever kons returned on the previous iteration, and k is the current index. The iteration is strictly left to right. The final state value is returned.
Find the longest string's length in vector-of-strings.
(vector-fold (lambda (len str)
(max (string-length str) len))
0 vector-of-strings)
Produce a list of the reversed elements of vector.
(vector-fold (lambda (tail elt) (cons elt tail))
'() vector)
Count the number of even numbers in vector.
(vector-fold (lambda (counter n)
(if (even? n) (+ counter 1) counter))
0 vector)
Similar to vector-fold, but iterates right to left instead of left to right.
Convert a vector to a list.
(vector-fold-right (lambda (tail elt) (cons elt tail))
'() vector)
f must accept as many arguments as there are vectors and return one value. It is an error if vector1 contains more elements than any of the other vectors.
Similar to vector-map, but the mapped elements are destructively inserted into vector1 rather than into a new vector. The dynamic order of application of f is unspecified.
Note: Because the order of application is unspecified, it is dangerous for f to apply vector-ref or vector-set! to vector1.
Counts the number of parallel elements in the vectors that satisfy pred?. For each index from zero (inclusive) to the length of the shortest vector (exclusive), pred? is applied to the corresponding elements of the vectors, in order.
(vector-count even? '#(3 1 4 1 5 9 2 5 6))⇒3
(vector-count < '#(1 3 6 9) '#(2 4 6 8 10 12))⇒2
f must accept two arguments and return one value.
Returns a newly allocated vector with the same length as vector. Each element of the new vector is set to the result of invoking f on the preceding element of the new vector and the corresponding element of vector, except that for the first call to f, the first argument is knil.
(vector-cumulate + 0 '#(3 1 4 1 5 9 2 5 6))⇒#(3 4 8 9 14 23 25 30 36)
Finds and returns the index of the first parallel elements in the vectors that satisfy pred?. If no matching elements are found by the end of the shortest vector, #f is returned.
(vector-index even? '#(3 1 4 1 5 9))⇒2
(vector-index < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))⇒1
(vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))⇒#f
All of the vectors must have the same length.
Like vector-index, but searches right to left rather than left to right.
Finds and returns the index of the first parallel elements in the vectors that do not satisfy pred?. If all the parallel elements satisfy pred? through the end of the shortest vector, returns #f. This is equivalent to:
(vector-index (lambda (obj1 obj2 ...)
(not (pred? obj1 obj2 ...)))
vector1 vector2 ...)
(vector-skip number? '#(1 2 a b 3 4 c d))⇒2
It is an error if the vectors do not all have the same length.
Like vector-skip, but searches for non-matching elements right to left rather than left to right. This is equivalent to:
(vector-index-right (lambda (obj1 obj2 ...)
(not (pred? obj1 obj2 ...)))
vector1 vector2 ...)
cmp must be a procedure of two arguments returning a negative integer if its first argument is less than its second, zero if they are equal, or a positive integer if its first argument is greater than its second.
The source does not explicitly require the vector to be sorted according to cmp. State the ordering precondition when editing this section.
Similar to vector-index and vector-index-right, but performs a binary search instead of searching left to right or right to left. Returns the index of an element of vector that matches value in the sense of cmp, or #f if no such element exists. If more than one element matches, the index of any of them may be returned.
Finds the first set of parallel elements in the vectors for which pred? returns a true value. If such a set exists, vector-any returns the value that pred? returned for that set of elements. The iteration is strictly left to right.
The source does not explicitly specify termination at the shortest vector or the result when no elements match (including empty vectors). Confirm that the result is #f in these cases.If, for every index from zero (inclusive) to the length of the shortest vector (exclusive), the set of corresponding elements satisfies pred?, vector-every returns the value that pred? returned for the last set of elements, at the last index of the shortest vector. The iteration is strictly left to right.
The source leaves the failure and empty-vector cases implicit. Confirm that iteration stops with #f at the first non-matching set and returns #t if any vector is empty.A vector the same size as vector is newly allocated and filled with all the elements of vector that satisfy pred? in their original order, followed by all the elements that do not satisfy pred?, also in their original order.
Two values are returned: the newly allocated vector and the number of elements that satisfy pred?.
k1 and k2 must be valid indices of vector.
Swaps the values of the locations in vector at k1 and k2.
Destructively reverses the contents of the sequence of locations in vector between start and end. start defaults to zero and end defaults to the length of vector.
Note: This does not deeply reverse.
Like vector-copy!, but the elements appear in to in reverse order.
The source says only ‘Like vector-copy!’. Confirm that its temporary-vector semantics for overlapping source and destination also apply to the reverse copy.
Like vector-unfold, but the elements are copied into vector starting at element start rather than into a newly allocated vector. Terminates when (- end start) elements have been generated.
Like vector-unfold!, but the elements are copied in reverse order into vector starting at the index preceding end.
Like vector->list, but the resulting list contains the elements of vector in reverse order.
Like list->vector, but the resulting vector contains the elements of list in reverse order.
Copyright © Taylor Campbell (2003). All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.