Next: , Previous: , Up: bytevectors 8   [Index]


27.2.9 Fold and unfold

Function: bytevector-s8-fold-left kons knil bv0 bv ...
Function: bytevector-u8-fold-left kons knil bv0 bv ...
Function: bytevector-s8-fold-right kons knil bv0 bv ...
Function: bytevector-u8-fold-right kons knil bv0 bv ...

The fundamental bytevector iterator. The bytevector arguments must have the same length.

kons is iterated left–to–right over each index in all of the bytevectors, stopping at the end of the shortest; kons is applied as:

(kons idx state
  (bytevector-u8-ref bv0 idx)
  (bytevector-u8-ref bv  idx)
  ยทยทยท)

where state is the current state value; the current state value begins with knil, and becomes whatever kons returned at the respective iteration; idx is the current index.

bytevector-u8-fold-right is similar to bytevector-u8-fold, but it iterates right–to–left.

Notice that to allow for an unspecified number of arguments, these folds hand the state as first argument to kons, as opposed to the usual fold arguments.

Function: bytevector-s8-fold-left* kons knil bv0 bv ...
Function: bytevector-u8-fold-left* kons knil bv0 bv ...
Function: bytevector-s8-fold-right* kons knil bv0 bv ...
Function: bytevector-u8-fold-right* kons knil bv0 bv ...

Like bytevector-u8-fold and bytevector-u8-unfold but accept bytevectors of different length, iterating until the end of the shortest one.

Function: %subbytevector-s8-fold-left kons knil bv start past
Function: %subbytevector-u8-fold-left kons knil bv start past
Function: %subbytevector-s8-fold-right kons knil bv start past
Function: %subbytevector-u8-fold-right kons knil bv start past
Macro: bytevector-s8-fold kons knil S
Macro: bytevector-u8-fold kons knil S
Macro: bytevector-s8-fold-right kons knil S
Macro: bytevector-u8-fold-right kons knil S

Fundamental iterators for subvectors. kons is iterated over each byte of the selected subvector:

(kons
  (bytevector-u8-ref bv (+ start idx))
  state)

where state is the current state value; the current state value begins with knil, and becomes whatever kons returned at the respective iteration; idx is the current index.

The left–fold iterator, %subbytevector-u8-fold-left, builds the return value as:

(kons
  (bytevector-u8-ref bv (- past 1))
  (kons
    (bytevector-u8-ref bv (- past 2))
    ...
      (kons
        (bytevector-u8-ref bv (+ start 2))
        (kons
           (bytevector-u8-ref bv (+ start 1))
           (kons
              (bytevector-u8-ref bv start)
              knil)))))

The right–fold iterator, %subbytevector-u8-fold-right, builds the return value as:

(kons
  (bytevector-u8-ref bv start
  (kons
    (bytevector-u8-ref bv (+ start 1))
    ...
      (kons
        (bytevector-u8-ref bv (- past 3))
        (kons
           (bytevector-u8-ref bv (- past 2))
           (kons
              (bytevector-u8-ref bv (- past 1))
              knil)))))
Function: bytevector-s8-unfold stop? seed->char make-seed first-seed
Function: bytevector-u8-unfold stop? seed->char make-seed first-seed
Function: bytevector-s8-unfold stop? seed->char make-seed first-seed base-bv
Function: bytevector-u8-unfold stop? seed->char make-seed first-seed base-bv
Function: bytevector-s8-unfold stop? seed->char make-seed first-seed base-bv make-final
Function: bytevector-u8-unfold stop? seed->char make-seed first-seed base-bv make-final

This is a fundamental constructor for bytevectors. Arguments description follows.

make-seed

A map function used to generate a series of “seed” values from the initial seed:

first-seed
(make-seed first-seed)            ⇒ seed2
(make-seed seed2)                 ⇒ seed3
(make-seed seed3)                 ⇒ seed4
...
stop?

A predicate function telling when to stop generating bytes; when it returns true when applied to one of the seed values.

seed->char

Map function mapping each seed value to the corresponding byte in the result bytevector. These bytes are assembled into the bytevector in a left–to–right order.

base-bv

An optional bytevector which is used as initial/leftmost portion of the constructed bytevector. Defaults to the empty bytevector.

make-final

Optional function applied to the terminal seed value (on which stop? returns true) to produce the final/rightmost portion of the constructed bytevector. Defaults to (lambda (x) "").

The final bytevector constructed does not share storage with either base-bv or the value produced by make-final.

Function: bytevector-s8-unfold-right stop? seed->char make-seed first-seed
Function: bytevector-u8-unfold-right stop? seed->char make-seed first-seed
Function: bytevector-s8-unfold-right stop? seed->char make-seed first-seed base-bv
Function: bytevector-u8-unfold-right stop? seed->char make-seed first-seed base-bv
Function: bytevector-s8-unfold-right stop? seed->char make-seed first-seed base-bv make-final
Function: bytevector-u8-unfold-right stop? seed->char make-seed first-seed base-bv make-final

This is a fundamental constructor for bytevectors. The arguments are like the ones of bytevector-u8-unfold. The difference from bytevector-u8-unfold is that this function builds the bytevector from right to left.

The final bytevector constructed does not share storage with either base-bv or the value produced by make-final.


Next: , Previous: , Up: bytevectors 8   [Index]