iter

package
v7.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2023 License: Apache-2.0, Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IterResult

type IterResult[T any] struct {
	Item     T
	Err      error
	Finished bool
}

IterResult is the result of try to advancing an impure iterator. Generally it is a "sum type", which only one field would be filled. You can create it via `Done`, `Emit` and `Throw`.

func CollectAll

func CollectAll[T any](ctx context.Context, it TryNextor[T]) IterResult[[]T]

CollectAll fully consumes the iterator, collecting all items the iterator emitted. When the iterator has been finished, it emits empty slice and won't set `Finished`.

func CollectMany

func CollectMany[T any](ctx context.Context, it TryNextor[T], n uint) IterResult[[]T]

CollectMany collects the first n items of the iterator. When the iterator contains less data than N, it emits as many items as it can and won't set `Finished`.

func Done

func Done[T any]() IterResult[T]

Done creates an IterResult which indices the iteration has finished.

func DoneBy

func DoneBy[T, O any](r IterResult[O]) IterResult[T]

DoneBy creates a finished or error IterResult by its argument.

func Emit

func Emit[T any](t T) IterResult[T]

Emit creates an IterResult which contains normal data.

func Throw

func Throw[T any](err error) IterResult[T]

Throw creates an IterResult which contains the err.

func (IterResult[T]) FinishedOrError

func (r IterResult[T]) FinishedOrError() bool

func (IterResult[T]) String

func (r IterResult[T]) String() string

type TransformConfig

type TransformConfig func(*chunkMappingCfg)

TransformConfig is the config for the combinator "transform".

func WithChunkSize

func WithChunkSize(n uint) TransformConfig

func WithConcurrency

func WithConcurrency(n uint) TransformConfig

type TryNextor

type TryNextor[T any] interface {
	TryNext(ctx context.Context) IterResult[T]
}

TryNextor is the general interface for "impure" iterators: which may trigger some error or block the caller when advancing.

func ConcatAll

func ConcatAll[T any](items ...TryNextor[T]) TryNextor[T]

ConcatAll concatenates all elements yields by the iterators. In another word, it 'chains' all the input iterators.

func Fail

func Fail[T any](err error) TryNextor[T]

Fail creates an iterator always fail.

func FilterOut

func FilterOut[T any](it TryNextor[T], f func(T) bool) TryNextor[T]

FilterOut returns an iterator that yields all elements the original iterator generated and DOESN'T satisfies the predicate.

func FlatMap

func FlatMap[T, R any](it TryNextor[T], mapper func(T) TryNextor[R]) TryNextor[R]

FlapMap applies the mapper over every elements the origin iterator generates, then flatten them.

func FromSlice

func FromSlice[T any](s []T) TryNextor[T]

FromSlice creates an iterator from a slice, the iterator would

func Map

func Map[T, R any](it TryNextor[T], mapper func(T) R) TryNextor[R]

Map applies the mapper over every elements the origin iterator yields.

func OfRange

func OfRange[T constraints.Integer](begin, end T) TryNextor[T]

OfRange creates an iterator that yields elements in the integer range.

func TakeFirst

func TakeFirst[T any](inner TryNextor[T], n uint) TryNextor[T]

TakeFirst takes the first n elements of the iterator.

func Tap

func Tap[T any](i TryNextor[T], with func(T)) TryNextor[T]

Tap adds a hook into the iterator, it would execute the function anytime the iterator emits an item.

func Transform

func Transform[T, R any](it TryNextor[T], with func(context.Context, T) (R, error), cs ...TransformConfig) TryNextor[R]

Transform returns an iterator that performs an impure procedure for each element, then emitting the result of that procedure. The execution of that procedure can be paralleled with the config `WithConcurrency`. You may also need to config the `WithChunkSize`, because the concurrent execution is only available intra-batch.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL