Folding v0.1.0

Source
Reducing a slice to one value, and mapping one slice onto another.

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.

Source

Type parameters

NameDescription
Tthe element type
Athe accumulated value's type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on
initialAthe value to start from
combinefunc(A, T) -> Ahow 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.

Source

Type parameters

NameDescription
Tthe element type
Athe accumulated value's type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on
initialAthe value to start from
combinefunc(A, T) -> Ahow 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.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on
initialTthe 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.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on
initialTthe 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.

Source

Type parameters

NameDescription
Tthe element type
Uthe destination element type

Parameters

NameTypeDescription
destinationvar U[..]where the results go
sourceT[..]where the elements come from
transformfunc(T) -> Uhow to turn a source element into a destination one

Returns: whether destination was long enough to hold every result