Next: vectors fold sub, Up: vectors fold [Index]
Fold the function combine over the vector arguments, which must have the same length. The return value is the return value of the last evaluated call to combine; if all the vector arguments are empty, the return value is knil. The syntaxes may be a little faster in the multiple vector arguments case.
The left–folding variants iterate combine left–to–right over each element of equal index in all the vectors; combine is applied as:
(combine state (vector-ref vec0 idx) (vector-ref vec idx) ···)
where state is the current state value, and it is the first argument; the current state value begins with knil, and becomes whatever combine returned at the respective iteration.
The right–folding variants iterate combine right–to–left over each element of equal index in all the vectors; combine is applied as:
(combine (vector-ref vec0 idx) (vector-ref vec idx) ··· state)
in which state is the last argument.
Like the above functions, but accept vectors of different length, iterating until the end of the shortest.
Folding operators variants which apply combine using the index of the current elements as first argument. For the left–folding operators:
(combine idx state (vector-ref vec0 idx) (vector-ref vec idx) ···)
for the right–folding operators:
(combine idx (vector-ref vec0 idx) (vector-ref vec idx) ··· state)
Here is a possible implementation for vector-map!
:
(define (vector-map! proc vec0 . vectors) (apply vector-fold-left/with-index (lambda (index state . items) (vector-set! state index (apply proc index items)) state) vec0 vec0 vectors))
Like the functions above, but stop the iteration if combine
returns #f
. The return value is the return value of the last
evaluated call to combine; if all the vector arguments are empty,
the return value is knil.
These functions can be used to implement functions like
vector-every
:
(define (vector-every pred vec0 . vectors) (apply vector-and-fold-left (lambda (state . items) (apply pred items)) #t ;must be true (cons vec0 vectors)))
Like the functions above, but stop the iteration if combine returns true as state. The return value is the return value of the last evaluated call to combine. If all the vectors are empty, the return value is knil.
These functions can be used to implement functions like
vector-any
:
(define (vector-any pred vec0 . vectors) (apply vector-or-fold-left (lambda (state . items) (apply pred items)) #f ;must be false (cons vec0 vectors)))
Apply pred to adjacent couples of elements from vec; return
true if all the evaluations of pred were true. The iteration
stops at the first #f
return value from pred.
This function is implemented as:
(define (fold-left/pred pred knil ell) (vector-and-fold-left/stx (lambda (state item) (and (pred state item) item)) knil ell))
and it can be used to implement predicates for ordering like <
:
(vector-fold-left/pred < 0 '(1 2 3 4 5 6)) ⇒ 6 (vector-fold-left/pred < 0 '(1 2 3 -4 5 6)) ⇒ #f
Next: vectors fold sub, Up: vectors fold [Index]