[ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

## 11.13 `srfi-43` - Vector library

Module: srfi-43

This module provides library functions for vectors. Some of srfi-43 procedures are built-in; see Vectors.

### Vector constructors

Function: vector-unfold f length seed …

[SRFI-43] Creates a vector of length length, filling elements left to right by calling f repeatedly.

The procedure f must take as many arguments as one plus number of seed values, and must return the same number of values. The first argument is the index. The first return value is used for the element of the result vector, and the rest of return values are passed to the next call of f.

 ```(vector-unfold (^[i x] (values (cons i x) (* x 2))) 8 1) ⇒ #((0 . 1) (1 . 2) (2 . 4) (3 . 8) (4 . 16) (5 . 32) (6 . 64) (7 . 128)) ```
Function: vector-unfold-right f length seed …

[SRFI-43] Creates a vector of length length, filling elements right to left by calling f repeatedly.

The procedure f must take as many arguments as one plus number of seed values, and must return the same number of values. The first argument is the index. The first return value is used for the element of the result vector, and the rest of return values are passed to the next call of f.

 ```(vector-unfold-right (^[i x] (values (cons i x) (* x 2))) 8 1) ⇒ #((0 . 128) (1 . 64) (2 . 32) (3 . 16) (4 . 8) (5 . 4) (6 . 2) (7 . 1)) ```
Function: vector-reverse-copy vec :optional start end

[SRFI-43] Copies the vector vec with reversing its elements. Optional start and end arguments can limit the range of the input.

 ```(vector-reverse-copy '#(a b c d e) 1 4) ⇒ #(d c b) ```
Function: vector-append vec …

[SRFI-43] Returns a newly allocated vector whose contents are concatenation of elements of vec in order.

Function: vector-concatenate list-of-vectors

[SRFI-43] Acts like `(apply vector-append list-of-vectors)`, though this may be more efficient.

### Vector predicates

Function: vector-empty? vec

[SRFI-43] Returns `#t` if vec’s length is zero, and `#f` if vec’s length is more than zero. Signals an error if vec is not a vector.

Function: vector= elt= vec …

[SRFI-43] Compares vecs element-wise, using given predicate elt=. Returns `#t` iff lengths of all the vectors are the same, and every corresponding elements are equal by elt=. Elt= is always called with two arguments and must return `#t` iff two are the same.

### Iteration over vectors

Function: vector-fold kons knil vec1 vec2 …

[SRFI-43] Kons is a procedure that takes n+2 arguments, where n is the number of given vectors. For each element of the given vectors, kons is called as `(kons i seed e_1i e_2i …)`, where i is the index, and e_ni is the i-th element of the vector n. If the lengths of the vectors differ, iteration stops when the shortest vector is exhausted.

The initial value of seed is knil, and the return value from kons is used as the next seed value. The last return value of kons is returned from `vector-fold`.

The iteration is strictly left to right.

Note that the seed value precedes elements, which is opposite to `fold` (See section Mapping over collection). It’s an unfortunate historical glitch; `vector-fold-left` would be more consistent name.

Function: vector-fold-right kons knil vec1 vec2 …

[SRFI-43] Like `vector-fold`, but iterates right to left.

Function: vector-map f vec1 vec2 …
Function: vector-map! f vec1 vec2 …
Function: vector-for-each f vec1 vec2 …
Function: vector-count f vec1 vec2 …

[SRFI-43] The argument f is a procedure that takes n+1 arguments where n is the number of given vectors. These procedures call f for each element of the given vectors as `(f i e_1i e_2i …)`, where i is the index, and e_ni is the i-th element of the vector n. If the lengths of the vectors differ, iteration only covers the shortest vector.

`vector-map` creates a fresh vector out of the result of applications of f and returns it. `vector-map!` reuses vec1 to store the result and returns vec1; hence vec1 must be mutable.

`vector-for-each` is for side-effects. It discards the result of f.

`vector-count` counts the number of times f returned a true value.

 ```(vector-map list '#(a b c)) ⇒ #((0 a) (1 b) (2 c)) (vector-map list '#(a b c) '#(d e f g)) ⇒ #((0 a d) (1 b e) (2 c f)) (vector-count = '#(0 2 2 4 4)) ⇒ 3 ```

Note: Gauche has builtin `vector-map`, `vector-map!` and `vector-for-each`, with slightly different API (no index is passed to f). R7RS also defines `vector-map` and `vector-for-each` that doesn’t pass index. For new development, we recommend using R7RS/Gauche-native `vector-map` etc. If you want to use Gauche-native `vector-map` and `srfi-43` together, you can import `srfi-43` with `:except (vector-map vector-map! vector-for-each)` option. Gauche provides builtin `vector-map-with-index` etc., which is the same as SRFI-43’s `vector-map` etc. See section Vectors, for the details.

Note: The generic `map` and `for-each` in `gauche.collection` can be used on vectors, but the procedure is called without index, and the result is returned as a list. `(vector-map f vec1 vec2 …)` is operationally equivalent to `(map-to-with-index <vector> f vec1 vec2 …)`. See `gauche.collection` - Collection framework and `gauche.sequence` - Sequence framework.

### Vector searching

Function: vector-index pred vec1 vec2 …
Function: vector-index-right pred vec1 vec2 …

[SRFI-43] Returns the index of the first or the last elements in vec1 vec2 … that satisfy pred, respectively. Returns `#f` if no elements satisfy pred. In `vector-index`, comparison ends at the end of the shortest vector. For `vector-index-right`, all the vectors must have the same length.

Function: vector-skip pred vec1 vec2 …
Function: vector-skip-right pred vec1 vec2 …

[SRFI-43] Like `vector-index` and `vector-index-right`, except that the result of pred is negated. That is, returns the index of the first or the last elements that don’t sastisfy pred.

Function: vector-binary-search vec value cmp :optional start end

[SRFI-43+] Look for value in a vector vec, and returns its index if it is found, or `#f` if it is not found. Comparison of value and an element in vec is done by a procedure cmp, which takes two arguments, and should return a negative integer if the first argument is less than the second, 0 if they are the same, and a positive integer if the first is greater than the second.

Elements in vec must be ordered from smaller to greater w.r.t. cmp. Using that fact, this procedure performs binary search instead of linear search.

The optional arguments start and end are an extention to SRFI-43, and can be used to limit the range of the search in start-th element (inclusive) to end-th element (exclusive).

Function: vector-any pred vec1 vec2 …

[SRFI-43] Applies pred on each corresponding elements of vec1 vec2 … left to right, and as soon as pred returns non-`#f` value, the procedure stops iteration and returns the value.

If no elements that satisfy pred are found, it returns `#f`.

Vectors can have different lengths. Iteration stops at the end of the shortest.

Function: vector-every pred vec1 vec2 …

[SRFI-43] Applies pred on each corresponding elements of vec1 vec2 … left to right. If all the elements (when the lengths of vectors differ, the first N elements where N is the length of the shortest) satisfy pred, returns the last result of pred. Otherwise returns `#f`.

### Vector mutators

Function: vector-swap! vec i j

[SRFI-43] Swaps vector vec’s `i`-th and `j`-th elements.

Function: vector-reverse! vec :optional start end

[SRFI-43] Reverse the elements of vec. Returns an undefined value. Optional start and end arguments can limit the range of operation.

 ```(rlet1 v (vector 'a 'b 'c 'd 'e) (vector-reverse! v 0 4)) ⇒ #(d c b a e) ```
Function: vector-copy! target tstart source :optional sstart send

[SRFI-43] Copies the content of source vector intto the target vector starting from tstart in the target. Optional sstart and send limits the range of source vector.

 ```(rlet1 v (vector 'a 'b 'c 'd 'e) (vector-copy! v 2 '#(1 2))) ⇒ #(a b 1 2 e) ```
Function: vector-reverse-copy! target tstart source :optional sstart send

[SRFI-43] Like `vector-copy!`, but reverses the order of elements from start.

 ```(rlet1 v (vector 'a 'b 'c 'd 'e) (vector-reverse-copy! v 2 '#(1 2))) ⇒ #(a b 2 1 e) ```

### Vector conversion

Function: reverse-vector->list vec :optional start end

[SRFI-43] Same as `(reverse (vector->list vec start end))`, but more efficient.

Function: reverse-list->vector list :optional start end

[SRFI-43] Same as `(reverse (list->vector list start end))`, but more efficient.

 [ < ] [ > ] [ << ] [ Up ] [ >> ]

This document was generated on July 19, 2014 using texi2html 1.82.