Folding v0.1.0
Fold<T, A>
pub func Fold<T, A>(
items: T[..],
initial: A,
combine: func(A, T) -> A
) -> A
Folds every element of items into an accumulator, left to right.
items is borrowed and read; initial is read by value and returned when the sequence is empty; combine is
called once per element with the accumulator so far and the element. The accumulator's type is independent of the
element's.
Left to right is part of the contract rather than an implementation detail: a combine that is not associative
gives a different answer in the other direction, and callers depend on the order.
Cannot fail. Runs in time proportional to the length and allocates nothing beyond whatever combine does.
Type parameters
| Name | Description |
|---|---|
T | the element type |
A | the accumulated value's type |
Parameters
| Name | Type | Description |
|---|---|---|
items | T[..] | the sequence to work on |
initial | A | the value to start from |
combine | func(A, T) -> A | how to fold an element into the running value |
Returns: the value left after folding every element into it
FoldRight<T, A>
pub func FoldRight<T, A>(
items: T[..],
initial: A,
combine: func(A, T) -> A
) -> A
Folds every element of items into an accumulator, right to left.
items is borrowed and read; initial is read by value and returned when the sequence is empty; combine is
called once per element with the accumulator so far and the element, starting from the last.
The mirror of Fold, and a different answer for any combine that is not associative.
Cannot fail. Runs in time proportional to the length and allocates nothing beyond whatever combine does.
Type parameters
| Name | Description |
|---|---|
T | the element type |
A | the accumulated value's type |
Parameters
| Name | Type | Description |
|---|---|---|
items | T[..] | the sequence to work on |
initial | A | the value to start from |
combine | func(A, T) -> A | how to fold an element into the running value |
Returns: the value left after folding every element into it, from the end
Sum<T>
pub func Sum<T>(items: T[..], initial: T) -> T
The sum of initial and every element of items.
items is borrowed and read; initial is read by value and is what an empty sequence gives back. The starting
value is asked for rather than assumed, because a generic element type has no zero this package could name.
Requires + on the element type. Overflow behaves exactly as + does on that type: this does not check, and a
caller who needs checking folds a checked addition instead.
Cannot fail. Runs in time proportional to the length and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
items | T[..] | the sequence to work on |
initial | T | the value to start from |
Returns: initial plus every element
Product<T>
pub func Product<T>(items: T[..], initial: T) -> T
The product of initial and every element of items.
items is borrowed and read; initial is read by value and is what an empty sequence gives back — which is why
it must be one rather than zero for the answer to mean anything.
Requires * on the element type. Overflow behaves exactly as * does on that type.
Cannot fail. Runs in time proportional to the length and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
items | T[..] | the sequence to work on |
initial | T | the value to start from |
Returns: initial times every element
Transform<T, U>
pub func Transform<T, U>(
destination: var U[..],
source: T[..],
transform: func(T) -> U
) -> bool
Writes transform of each element of source into destination.
source is borrowed and read; destination is borrowed mutably and written; transform is called once per
element. The two sequences may have different element types, and may be the same storage when they do not.
Reports false, having written nothing, when the two lengths differ — for the same reason CopyTo does: a
partial result is worse than a refused one when the caller cannot tell how far it got.
Runs in time proportional to the length and allocates nothing beyond whatever transform does.
Type parameters
| Name | Description |
|---|---|
T | the element type |
U | the destination element type |
Parameters
| Name | Type | Description |
|---|---|---|
destination | var U[..] | where the results go |
source | T[..] | where the elements come from |
transform | func(T) -> U | how to turn a source element into a destination one |
Returns: whether destination was long enough to hold every result