chickadee » srfi-133 » vector-fold

vector-fold kons knil vec1 vec2 ...procedure

The fundamental vector iterator. kons is iterated over each value in all of the vectors, stopping at the end of the shortest; kons is applied as (kons 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 on the previous 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 (len str) (max (string-length str) len))
               0 vector-of-strings)

Produce a list of the reversed elements of vec.

(vector-fold (lambda (tail elt) (cons elt tail))
               '() vec)

Count the number of even numbers in vec.

(vector-fold (lambda (counter n)
                 (if (even? n) (+ counter 1) counter))
               0 vec)