chickadee » vector-lib » vector-fold

vector-fold kons knil vec_1 vec_2 ···procedure

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 vec_1 i) (vector-ref vec_2 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 (λ (index len str) (max (string-length str) len))
             0 vector-of-strings)

Produce a list of the reversed elements of vec.

(vector-fold (λ (index tail elt) (cons elt tail))
             '() vec)

Count the number of even numbers in vec.

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