Documentation
¶
Index ¶
- Variables
- func Append[S ~[]T, T any](dst S, s Sequence[T]) (S, error)
- func Collect[T, U any](s Sequence[T], initial U, process func(T, U) U) (U, error)
- func CollectErr[T, U any](s Sequence[T], initial U, process func(T, U) (U, error)) (U, error)
- func Each[T any](s Sequence[T]) func(func(T) error) error
- func EachSimple[T any](s Sequence[T]) func(func(T) bool) error
- func First[T any](s Sequence[T]) (T, error)
- func IntoChan[T any](ch chan<- T, s Sequence[T]) error
- func IntoChanCtx[T any](ctx context.Context, ch chan<- T, s Sequence[T]) error
- func IntoChanPair[T any](ch chan<- Pair[T, error], s Sequence[T])
- func Iterator[T any](s Sequence[T]) func(func(T) bool)
- func Last[T any](s Sequence[T]) (T, error)
- func ToChan[T any](s Sequence[T]) <-chan T
- func ToChanCtx[T any](ctx context.Context, s Sequence[T]) (<-chan T, context.Context)
- func ToChanErr[T any](s Sequence[T]) (<-chan T, <-chan error)
- func ToChanPair[T any](s Sequence[T]) <-chan Pair[T, error]
- func ToSlice[T any](s Sequence[T]) ([]T, error)
- type Pair
- type Sequence
- func AddFirst[A, T any](a A, s Sequence[T]) Sequence[Pair[A, T]]
- func AddSecond[T, B any](s Sequence[T], b B) Sequence[Pair[T, B]]
- func Concat[T any](seqs ...Sequence[T]) Sequence[T]
- func Error[T any](err error) Sequence[T]
- func Filter[T any](s Sequence[T], pred func(T) bool) Sequence[T]
- func FilterErr[T any](s Sequence[T], pred func(T) (bool, error)) Sequence[T]
- func FirstOnly[A, B any](s Sequence[Pair[A, B]]) Sequence[A]
- func Flatten[T any, S ~[]T](src Sequence[S]) Sequence[T]
- func FromChan[T any](ch <-chan T) Sequence[T]
- func FromSlice[T any](items []T) Sequence[T]
- func Generate[T any](f func(func(T) error) error) Sequence[T]
- func GenerateVolatile[T any](f func(func(T) error) error) Sequence[T]
- func Infinite[T any](t T) Sequence[T]
- func Limit[T any](s Sequence[T], n int) Sequence[T]
- func Map[In, Out any](s Sequence[In], convert func(In) Out) Sequence[Out]
- func MapErr[In, Out any](s Sequence[In], convert func(In) (Out, error)) Sequence[Out]
- func MapFilter[In, Out any](s Sequence[In], convert func(In) (Out, bool, error)) Sequence[Out]
- func Materialize[T any](s Sequence[T]) Sequence[T]
- func New[T any](items ...T) Sequence[T]
- func Process[In, Out any](src Sequence[In], proc func(In, func(Out)) error) Sequence[Out]
- func Repeat[T any](t T, n int) Sequence[T]
- func SecondOnly[A, B any](s Sequence[Pair[A, B]]) Sequence[B]
- func Single[T any](t T) Sequence[T]
- func SwapPairs[A, B any](s Sequence[Pair[A, B]]) Sequence[Pair[B, A]]
- func Volatile[T any](s Sequence[T]) Sequence[T]
- func Zip[A, B any](aSeq Sequence[A], bSeq Sequence[B]) Sequence[Pair[A, B]]
- func ZipShortest[A, B any](aSeq Sequence[A], bSeq Sequence[B]) Sequence[Pair[A, B]]
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 Append ¶ added in v0.0.5
Append processes the given sequence such that each item returned is appended to the provided destination slice. The resulting final slice is returned.
func Collect ¶ added in v0.0.5
Collect produces a single value from a sequence. It starts with an initial output value, and for every item in the sequence the value is permuted by the process function using the item from the sequence and the current value.
func CollectErr ¶ added in v0.0.5
CollectErr produces a single value from a sequence. It starts with an initial output value, and for every item in the sequence the value is permuted by the process function using the item from the sequence and the current value.
The callback may return errors to stop processing, either to indicate failure, or in the case of ErrStopIteration to simply end processing.
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 First ¶ added in v0.0.5
First returns the first value from the given sequence, or an error if even that isn't possible.
func IntoChan ¶ added in v0.0.5
IntoChan sends the contents of the sequence into the channel. If an error occurs processing the sequence it will be returned.
func IntoChanCtx ¶ added in v0.0.5
IntoChanCtx sends the contents of the sequence into the channel. If an error occurs processing the sequence it will be returned. The provided context will be consulted for an alternate reason to stop iteration.
func IntoChanPair ¶ added in v0.0.5
IntoChanPair sends the items from a sequence into a channel of type Pair[T,error] for the purpose of passing on the error from the sequence should one occur. If such an error happens it will be in a <zero,error> element on its own.
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.
func Last ¶ added in v0.0.5
Last returns the final value from the given sequence and any error should the sequence end with an error. Unlike most functions both value and error will be returns as it's really upto the caller to determine if an error should stop downstream processing in this case. Note: ErrStopIteration will still be suppressed as normal as it indicates early exit without error.
func ToChan ¶ added in v0.0.5
ToChan returns a channel that will receive the values from the sequence. If the sequence produces an error, then this code will panic.
func ToChanCtx ¶ added in v0.0.5
ToChanCtx returns a channel and a context where the channel returns values emitted from the input sequence, and the context will be cancelled if the sequence returns an error. An input context is used as the basis of the generated context, and can be used to cancel processing externally.
func ToChanErr ¶ added in v0.0.5
ToChanErr returns 2 channels, the first containing items from the sequence, and the second will receive an error if the sequence failed with that error.
func ToChanPair ¶ added in v0.0.5
ToChanPair returns a channel whose elements are Pair[T,error] such that errors that arise while processing the sequence will return a <zero,err> Pair.
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 AddFirst ¶ added in v0.0.5
AddFirst creates a sequence where each item is a pair of a constant value and a value from a given sequence. The constant value is the first item in the pair.
func AddSecond ¶ added in v0.0.5
AddSecond creates a sequence where each item is a pair of a constant value and a value from a given sequence. The constant value is the second item in the pair.
func Concat ¶ added in v0.0.5
Concat performs a concatenation of multiple sequences, where each sequence is iterated in turn until the final one is completed.
func Error ¶ added in v0.0.5
Error returns a sequence where the given error is return when iterated upon.
func Filter ¶ added in v0.0.5
Filter takes an input sequence and creates a sequence where only the values that pass the provided predicate function will be emitted. The ouput sequence will be the same type as the input sequence.
func FilterErr ¶ added in v0.0.5
FilterErr takes an input sequence and creates a sequence where only the values that pass the provided predicate function will be emitted. The ouput sequence will have the same element type as the input sequence.
FilterErr allows the callback to stop iteration with a generic error, or use ErrStopIteration to simply indicate that no more values should be proceed.
func FirstOnly ¶ added in v0.0.5
FirstOnly takes a sequence of Pair values, and returns a sequence of just the first value from each pair.
func Flatten ¶ added in v0.0.5
Flatten processes a sequence of slices, producing a new sequence of the element type, and iterating across each slice as it's pulled from the input.
func FromChan ¶ added in v0.0.5
FromChan produces a sequence from the provided channel. The new sequence is volatile since the channel itself can only be iterated over once.
func FromSlice ¶ added in v0.0.5
FromSlice returns a sequence where the elements of the slice are returned. The source slice is reference from the sequence so certain changes to that slice may affect the sequence.
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 Infinite ¶ added in v0.0.5
Infinite generates a sequence where the given value is returned forever, on each iteration of the sequence.
func Limit ¶ added in v0.0.5
Limit returns a sequence where only a given number of items can be accessed from the input sequence before hitting the end of the sequence.
func Map ¶ added in v0.0.5
Map takes in an input sequence and returns a sequence where every input value is permuted by the provided convert function. The new sequence may be of a different type due to the conversion, but will have the same number of items.
func MapErr ¶ added in v0.0.5
MapErr takes in an input sequence and returns a sequence where every input value is permuted by the provided convert function. The new sequence may be of a different type due to the conversion, but will have the same number of items.
MapErr allows the callback to stop iteration with a generic error, or use ErrStopIteration to simply indicate that no more values should be proceed.
func MapFilter ¶ added in v0.0.5
MapFilter performs both a Map and Filter operation on the input sequence where the convert function returns both the new value, and a boolean to indicate if it should be added at all.
MapFilter allows the callback to stop iteration with a generic error, or use ErrStopIteration to simply indicate that no more values should be proceed.
func Materialize ¶ added in v0.0.5
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.
func Process ¶ added in v0.0.5
Process provides a method of generating a destination sequence from a given input sequence using a BEAM style emitter.
The proc callback receives each input item, one at a time, and calls the provided emmiter function to output zero, one, or many results from that single input value. It can also return an error to stop processing.
func Repeat ¶ added in v0.0.5
Repeat generates a sequence where the given item is returns a fixed number of times.
func SecondOnly ¶ added in v0.0.5
SecondOnly takes a sequence of Pair values, and returns a sequence of just the second value from each pair.
func SwapPairs ¶ added in v0.0.5
SwapPairs processes a sequence with Pair[A,B] elements to produce one where the elements in the pair are swapped (i.e. Pair[B,A]).
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 Zip ¶ added in v0.0.5
Zip combines two input sequences into a sequence of Pairs where each pair contains an item from the first sequence matched up with one from the second sequence. When one seqence ends, the zipped sequence continues producing Pairs where one element is the zero value until both sequences are finished.
func ZipShortest ¶ added in v0.0.5
ZipShortest is like Zip, but stops once one sequence is empty.
func (Sequence[T]) Concat ¶ added in v0.0.5
Concat is a helper method which calls the top level function Concat to build a combined sequence of receiver followed by the given sequence(s).
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 is a utility method that calls the package level function Materialize on this sequence.