Next: , Up: vectors fold   [Index]


24.7.1 Folding with R6RS style

Function: vector-fold-left combine knil vec0 vec ...
Function: vector-fold-right combine knil vec0 vec ...
Syntax: vector-fold-left/stx combine knil vec0 vec ...
Syntax: vector-fold-right/stx combine knil vec0 vec ...

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.

Function: vector-fold-left* combine knil vec0 vec ...
Function: vector-fold-right* combine knil vec0 vec ...
Syntax: vector-fold-left*/stx combine knil vec0 vec ...
Syntax: vector-fold-right*/stx combine knil vec0 vec ...

Like the above functions, but accept vectors of different length, iterating until the end of the shortest.

Function: vector-fold-left/with-index combine knil vec0 vec ...
Function: vector-fold-left*/with-index combine knil vec0 vec ...
Function: vector-fold-right/with-index combine knil vec0 vec ...
Function: vector-fold-right*/with-index combine knil vec0 vec ...

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))
Function: vector-and-fold-left combine knil vec0 vec ...
Function: vector-and-fold-left* combine knil vec0 vec ...
Function: vector-and-fold-right combine knil vec0 vec ...
Function: vector-and-fold-right* combine knil vec0 vec ...
Syntax: vector-and-fold-left/stx combine knil vec0 vec ...
Syntax: vector-and-fold-left*/stx combine knil vec0 vec ...
Syntax: vector-and-fold-right/stx combine knil vec0 vec ...
Syntax: vector-and-fold-right*/stx combine knil vec0 vec ...

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)))
Function: vector-or-fold-left combine knil vec0 vec ...
Function: vector-or-fold-left* combine knil vec0 vec ...
Function: vector-or-fold-right combine knil vec0 vec ...
Function: vector-or-fold-right* combine knil vec0 vec ...
Syntax: vector-or-fold-left/stx combine knil vec0 vec ...
Syntax: vector-or-fold-left*/stx combine knil vec0 vec ...
Syntax: vector-or-fold-right/stx combine knil vec0 vec ...
Syntax: vector-or-fold-right*/stx combine knil vec0 vec ...

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)))
Function: vector-fold-left/pred pred knil vec

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: , Up: vectors fold   [Index]