Next: , Previous: , Up: iklib vectors   [Index]


6.28.5 Iterating vectors

Function: vector-find proc vec

proc should accept one argument and return a single value. proc should not mutate vec. vector-find applies proc to the elements of vec in order. If proc returns a true value for an element, vector-find immediately returns that element. If proc returns #f for all elements of the vector, vector-find returns #f. proc is always called in the same dynamic environment as vector-find itself.

(vector-find even? '#(3 1 4 1 5 9))
⇒ 4

(vector-find even? '#(3 1 5 1 5 9))
⇒ #f
Function: vector-for-all proc vec1 vec2vecn
Function: vector-exists proc vec1 vec2vecn

The vecs should all have the same length, and proc should accept N arguments and return a single value. proc should not mutate the vec arguments.

For natural numbers i = 0, 1, …, the vector-for-all procedure successively applies proc to arguments x_i^1 … x_i^N, where x_i^j is the i-th element of vecj, until #f is returned.

If proc returns true values for all but the last element of vec1, vector-for-all performs a tail call of proc on the k-th elements, where k is the length of vec1. If proc returns #f on any set of elements, vector-for-all returns #f after the first such application of proc. If the vecs are all empty, vector-for-all returns #t.

(vector-for-all even? '#())             ⇒ #t
(vector-for-all even? '#(3 1 4 1 5 9))  ⇒ #f
(vector-for-all even? '#(2 4 14))       ⇒ #t
(vector-for-all (lambda (n)
                  (and (even? n) n))
                '#(2 4 14))
⇒ 14
(vector-for-all < '#(1 2 3) '#(2 3 4))  ⇒ #t
(vector-for-all < '#(1 2 4) '#(2 3 4))  ⇒ #f

For natural numbers i = 0, 1, …, the vector-exists procedure applies proc successively to arguments x_i^1 … x_i^n, where x_i^j is the i-th element of vecj, until a true value is returned.

If proc returns #f for all but the last elements of the vecs, vector-exists performs a tail call of proc on the k-th elements, where k is the length of vec1. If proc returns a true value on any set of elements, vector-exists returns that value after the first such application of proc. If the vecs are all empty, vector-exists returns #f.

(vector-exists even? '#(3 1 4 1 5 9))   ⇒ #t
(vector-exists even? '#(3 1 1 5 9))     ⇒ #f
(vector-exists even? '#())              ⇒ #f
(vector-exists (lambda (n)
                 (and (even? n) n))
               '#(2 1 4 14))
⇒ 2
(vector-exists < '#(1 2 4) '#(2 3 4))   ⇒ #t
(vector-exists > '#(1 2 3) '#(2 3 4))   ⇒ #f

proc is always called in the same dynamic environment as vector-for-all or, respectively, vector-exists itself.

Function: vector-fold-left combine knil vec0 vec
Function: vector-fold-right 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 left–folding variant 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 variant 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.


Next: , Previous: , Up: iklib vectors   [Index]