Documentation
¶
Overview ¶
Package itertool provides a set of functional helpers for managinging and using fun.Iterator implementations, including a parallel Map/Reduce, Merge, and other convenient tools.
Index ¶
- func Channel[T any](pipe <-chan T) fun.Iterator[T]
- func CollectChannel[T any](ctx context.Context, iter fun.Iterator[T]) <-chan T
- func CollectSlice[T any](ctx context.Context, iter fun.Iterator[T]) ([]T, error)
- func Filter[T any](ctx context.Context, iter fun.Iterator[T], ...) fun.Iterator[T]
- func ForEach[T any](ctx context.Context, iter fun.Iterator[T], fn func(context.Context, T) error) (err error)
- func Map[T any, O any](ctx context.Context, opts MapOptions, iter fun.Iterator[T], ...) fun.Iterator[O]
- func Merge[T any](ctx context.Context, iters ...fun.Iterator[T]) fun.Iterator[T]
- func Reduce[T any, O any](ctx context.Context, iter fun.Iterator[T], ...) (value O, err error)
- func Slice[T any](in []T) fun.Iterator[T]
- func Synchronize[T any](in fun.Iterator[T]) fun.Iterator[T]
- type MapOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Channel ¶
Channel produces an iterator for a specified channel. The iterator does not start any background threads.
func CollectChannel ¶
CollectChannel converts and iterator to a channel.
func CollectSlice ¶
CollectSlice converts an iterator to the slice of it's values.
In the case of an error in the underlying iterator the output slice will have the values encountered before the error.
func Filter ¶
func Filter[T any]( ctx context.Context, iter fun.Iterator[T], fn func(ctx context.Context, input T) (output T, include bool, err error), ) fun.Iterator[T]
Filter passes all objects in an iterator through the specified filter function. If the filter function errors, the operation aborts and the error is reported by the returned iterator's Close method. If the include boolean is true the result of the function is included in the output iterator, otherwise the operation is skipped.
The output iterator is produced iteratively as the returned iterator is consumed.
func ForEach ¶
func ForEach[T any](ctx context.Context, iter fun.Iterator[T], fn func(context.Context, T) error) (err error)
ForEach passes each item in the iterator through the specified handler function, return an error if the handler function errors.
func Map ¶
func Map[T any, O any]( ctx context.Context, opts MapOptions, iter fun.Iterator[T], mapper func(context.Context, T) (O, error), ) fun.Iterator[O]
Map provides an orthodox functional map implementation based around fun.Iterator. Operates in asynchronous/streaming manner, so that the output Iterator must be consumed. The zero values of IteratorMapOptions provide reasonable defaults for abort-on-error and single-threaded map operation.
If the mapper function errors, the result isn't included, but the errors would be aggregated and propagated to the `Close()` method of the resulting iterator. If there are more than one error (as is the case with a panic or with ContinueOnError semantics,) the error is an *erc.Stack object. Panics in the map function are converted to errors and handled according to the ContinueOnPanic option.
func Merge ¶
Merge combines a set of related iterators into a single iterator. Starts a thread to consume from each iterator and does not otherwise guarantee the iterator's order.
func Reduce ¶
func Reduce[T any, O any]( ctx context.Context, iter fun.Iterator[T], reducer func(context.Context, T, O) (O, error), initalValue O, ) (value O, err error)
Reduce processes an input iterator with a reduce function and outputs the final value. The initial value may be a zero or nil value.
Types ¶
type MapOptions ¶
type MapOptions struct {
// ContinueOnPanic forces the entire IteratorMap operation to
// halt when a single map function panics. All panics are
// converted to errors and propagated to the output iterator's
// Close() method.
ContinueOnPanic bool
// ContinueOnError allows a map function to return an error
// and allow the work of the IteratorMap operation to
// continue. Errors are aggregated propagated to the output
// iterator's Close() method.
ContinueOnError bool
// NumWorkers describes the number of parallel workers
// processing the incoming iterator items and running the map
// function. All values less than 1 are converted to 1. Any
// value greater than 1 will result in out-of-sequence results
// in the output iterator.
NumWorkers int
}
MapOptions describes the runtime options to the IteratorMap operation. The zero value of this struct provides a usable strict operation.