Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRepeatedUse = errors.New("volatile sequence used more than once")
ErrRepeatedUse is returned by a VolatileSequence if it's accessed more than once.
var ( // ErrStopIteration is used by iteration callbacks to indicate that // the iteration should be stopped early, but there was no other issue. ErrStopIteration = errors.New("iteration stopped") )
Functions ¶
func Each ¶
Each returns a function to iterate over the sequence. The callback to the produced function is for users to receive and handle each value from the sequence, and return an error if processing should stop. The first error produced by the sequence, either an error from the callback or an error producing values will be returned. The ErrStopIteration error will not be returned from the iteration function.
func EachSimple ¶
EachSimple is like Each, where a function is returned to iterate over the contents of the sequence, but the callback is in a simpler true/false form. An error generating a value will be returned, and iteration stopped. When the callback returns false, an ErrStopIteration error is passed through the sequence to anyone who cares about errors, but will be dropped before returning from EachSimple as it doesn't represent a real error.
func Iterator ¶
Iterator is a wrapper on EachSimple and is used to work with the upcoming language extension that allow functions in a for-range statement. Thus, when avaliable, one may write "for x := range Iterator(seq) {}" to use a sequence directly in a for-loop.
Errors when generating the contents of the sequence will result in a panic.
Types ¶
type Pair ¶
type Pair[A, B any] struct { // contains filtered or unexported fields }
A Pair is a 2 element tuple containing values of independant types.
func (Pair[A, B]) AB ¶
func (p Pair[A, B]) AB() (A, B)
AB returns both values from the pair in order.
type Sequence ¶
A Sequence represents a functionally immutable series of values. The sequence can only be used to fetch the values stored within, but the sequence itself can't be used to make changes.
func Generate ¶
Generate is used to create a sequence manually from a "sequence function". A sequence function represents the for loop that will produce all the values of the sequence, passing them to a callback. The callback returns an error either on a problem, or because the user of the data wishes to stop early (by sending ErrStopIteration). The generator is expected to stop immediately on a non-nill error from the callback, perform cleanup, and then return a non-nil error (preferably wrapping the error it was given) or the exact error it received.
The generator function should not handle ErrStopIteration itself, both for simplicity (unless it intends to wrap all errors), and to allow the top-level of the sequence processing to see it (or a wrapped version).
type VolatileSequence ¶
A VolatileSequence represents a sequence where the process of iterating over the sequence damages or changes it in a fundamental way. Since sequences are meant to be immutable, some cases can't uphold this requirement.
VolatileSequences are OK to be accessed once, but any further access will return ErrRepeatedUse immediately. They serve as an indication that the user may wish to wrap them using [process.Materialize] or [process.Buffer] if they think they may be used more than once.
func Volatile ¶
func Volatile[T any](s Sequence[T]) VolatileSequence[T]
Volatile wraps the provided sequence in the VolatileSequence type indicating it should only be iterated over a single time. Any sequence, even if not actually volatile can be wrapped, and will prevent repeat calls to Each.
The exact nature of the wrapped sequence is as follows:
- The first time it's used it will behave exactly as normal
- it can stop early, fail, or run to completion
- Any subsequent accesses will return an error immediately
- the behaviour is unaffected by the outcome of the first iteration
- Iterations concurrent with the first will also fail in the same way