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 ¶
type Sequence[T any] struct { // contains filtered or unexported fields }
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).
func GenerateVolatile ¶ added in v0.0.4
GenerateVolatile is a helper to do the following: Volatile(Generate(f)). It creates a volatile sequence from the provided sequence function.
func Volatile ¶
Volatile returns a new sequence that depends on the the original, but is marked volatile, meaning it should only be iterated over a single time. Any sequence, even if not actually volatile can be wrapped, and will prevent repeat iterations over the sequence.
The exact nature of the volatile 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.
func (Sequence[T]) Each ¶
Each iterates over every item in the sequence calling the passed callback with each item. An error returned from the callback, or one that arose from the processing of the sequence will be returned if they arise and iteration will stop. Unlike the top-level functions that iterate over sequences, this method will not on it's own handle ErrStopIteration and return it instead.
func (Sequence[T]) IsVolatile ¶ added in v0.0.4
IsVolatile returns true if the sequence is volatile, meaning that it should only be iterated over once. It is an error to call Each/EachSimple/Iterator more than once. Volatile sequences can be created with the [Volatlie] function.
func (Sequence[T]) Materialize ¶ added in v0.0.4
Materialize returns a new sequence where all the data (including any error) from the current sequence is cached and played back on each iteration.
This has 3 main benefits:
- It is likely faster than the original sequence
- It has no dependencies, not even on the original sequence
- It can be iterated over multiple times
The speed benefit only makes sense if you are going to iterate more than once, as Materialize otherwise requires the time to iterate the sequence to be created.
Otherwise, it's other main use is to overcome the limitations of volatile sequences.