Next: srfi vector spec search, Previous: srfi vector spec select, Up: srfi vector spec [Index]
The fundamental vector iterator. kons is iterated over each index in all of the vectors, stopping at the end of the shortest; kons is applied as:
(kons i state (vector-ref vec1 i) (vector-ref vec2 i) ···)
where state is the current state value; the current state value begins with knil, and becomes whatever kons returned at the respective iteration; and i is the current index.
The iteration is strictly left–to–right.
Examples:
;; Find the longest string's length in vector-of-strings. (vector-fold (lambda (index len str) (max (string-length str) len)) 0 vector-of-strings) ;; Produce a list of the reversed elements of vec. (vector-fold (lambda (index tail elt) (cons elt tail)) '() vec) ;; Count the number of even numbers in vec. (vector-fold (lambda (index counter n) (if (even? n) (+ counter 1) counter)) 0 vec)
Similar to vector-fold
, but it iterates right to left instead of
left to right.
Example:
;; Convert a vector to a list. (vector-fold-right (lambda (index tail elt) (cons elt tail)) '() '#(a b c d)) => (a b c d)
Construct a new vector of the shortest size of the vector arguments. Each element at index i of the new vector is mapped from the old vectors by:
(f i (vector-ref vec1 i) (vector-ref vec2 i) ···)
The dynamic order of application of f is unspecified.
Examples:
(vector-map (lambda (i x) (* x x)) (vector-unfold (lambda (i x) (values x (+ x 1))) 4 1)) => #(1 4 9 16) (vector-map (lambda (i x y) (* x y)) (vector-unfold (lambda (i x) (values x (+ x 1))) 5 1) (vector-unfold (lambda (i x) (values x (- x 1))) 5 5)) => #(5 8 9 8 5) (let ([count 0]) (vector-map (lambda (ignored-index ignored-elt) (set! count (+ count 1)) count) '#(a b))) => #(1 2) OR #(2 1) (vector-map (lambda (i elt) (+ i elt)) '#(1 2 3 4)) => #(1 3 5 7)
Similar to vector-map
, but rather than mapping the new elements
into a new vector, the new mapped elements are destructively inserted
into vec1. The dynamic order of application of f
unspecified, so it is dangerous for f to apply either
vector-ref
or vector-set!
to vec1 in f.
Simple vector iterator: apply f to each index in the range
[0, length)
, where length is the length of the
smallest vector argument passed, and the respective list of parallel
elements from vec1, vec2, … at that index. In
contrast with vector-map
, f is reliably applied to each
subsequent elements, starting at index 0, in the vectors.
Example:
(vector-for-each (lambda (i x) (display x) (newline)) => '#("foo" "bar" "baz" "quux" "zot"))
displays:
foo bar baz quux zot
Count the number of parallel elements in the vectors that satisfy
pred?, which is applied, for each index i in the range
[0, length)
, where length is the length of the
smallest vector argument, to i and each parallel element in the
vectors at that index, in order.
Examples:
(vector-count (lambda (i elt) (even? elt)) '#(3 1 4 1 5 9 2 5 6)) => 3 (vector-count (lambda (i x y) (< x y)) '#(1 3 6 9) '#(2 4 6 8 10 12)) => 2
Next: srfi vector spec search, Previous: srfi vector spec select, Up: srfi vector spec [Index]