iterator

package
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reduce

func Reduce[T any, V any](
	ctx context.Context,
	it Iterator[T],
	reducer func(V, T) (V, error),
) (ret V, err error)

Reduce combines all the elements in an iterator using a binary operation to produce a single value.

func ToSlice

func ToSlice[T any](ctx context.Context, it Iterator[T]) ([]T, error)

ToSlice consumes the given iterator and returns a slice with all of the elements produced.

Types

type Iterator

type Iterator[T any] interface {
	// Next returns the next element and true or nil and false
	// if there's no more elements in this Iterator. Err should be
	// checked for any errors incurred during the lifecycle of this Iterator.
	// Note that if an error is found, it's up to the implementation as to
	// whether to return false and stop iteration or aggregate errors
	// and return at the end.
	//
	// This method blocks until data is available. Implementations should
	// use the given context's Done channel to know when data is no longer
	// required and so the call should return.
	Next(context.Context) (T, bool)
	// Err returns the first error or an aggreation of the errors
	// encountered by the Iterator.
	Err() error

	io.Closer
}

Iterator provides a convenient interface for iterating over chunks of structured or unstructured data such as a file of newline-delimited lines of text or a set of datastore documents.

func Aggregate

func Aggregate[T any](its ...Iterator[T]) Iterator[T]

Aggregate combines multiple iterators of T into one single iterator of T. If its is nil or empty, this method safely returns an empty iterator.

func Empty

func Empty[T any]() Iterator[T]

Empty returns an empty iterator.

func Error

func Error[T any](err error) Iterator[T]

Error returns an Iterator of T that never returns a value and always returns the given error.

func Filter

func Filter[T any](it Iterator[T], fn func(T) bool) Iterator[T]

Filter wraps an iterator of type T and returns another iterator that will apply fn to each of the elements produced and filter out any elements for which fn returns false.

func FromDocumentIterator

func FromDocumentIterator[T any](it storageapi.Iterator) Iterator[T]

FromDocumentIterator maps a storageapi.Iterator to an Iterator of type T.

func FromFunc

func FromFunc[T any](
	fn func(context.Context) (T, bool, error),
	close func() error,
) Iterator[T]

FromFunc returns an Iterator backed by the provided function.

func FromSlice

func FromSlice[T any](els []T) Iterator[T]

FromSlice returns an iterator backed by the given slice of elements.

func IsEmpty

func IsEmpty[T any](ctx context.Context, i Iterator[T]) (Iterator[T], bool)

IsEmpty consumes the first element in i and returns true if it is empty or false if not and returns a new iterator that should be used instead of i.

The given iterator is consumed in either case, so its Close method is wrapped with the returned iterator, or if the given iterator is empty, its Close method is called for the caller, so it's safe to override the variable holding the passed iterator with the return value of this function.

func Map

func Map[T any, V any](it Iterator[T], fn func(T) V) Iterator[V]

Map maps an iterator of type T and returns another iterator that will apply fn to each of the elements produced.

func Unslice

func Unslice[T any](it Iterator[[]T]) Iterator[T]

Unslice converts an iterator of slices of T into an iterator of T.

Jump to

Keyboard shortcuts

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