iterator

package module
v0.0.0-...-80cf31c Latest Latest
Warning

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

Go to latest
Published: May 11, 2022 License: MIT Imports: 3 Imported by: 8

Documentation

Overview

iterator provides the Collection and Iterator interfaces, as well as supporting functions (e.g. zip, map, filter, enumerate, etc.).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](iter Iterator[T], pred FilterFunc[T]) (bool, error)

All returns true if for all items in the iterator, pred returns true. Returns false, error upon collection error.

func Any

func Any[T any](iter Iterator[T], pred FilterFunc[T]) (bool, error)

Any returns true if for any item in the iterator, pred returns true. Returns false, error upon collection error.

func Fold

func Fold[T any, U any](iter Iterator[T], init *U, fn func(*U, T)) (*U, error)

Fold functions similarly to Reduce, except that an initial value is provided.

func ForEach

func ForEach[T any](iter Iterator[T], fn func(*T)) error

ForEach calls fn with each item in the iterator. Returns error on collection error

func None

func None[T any](iter Iterator[T], pred FilterFunc[T]) (bool, error)

None returns true if for all items in the iterator, pred returns false. Returns false, error upon collection error.

func Reduce

func Reduce[T, U any](iter Iterator[T], fn func(*U, T)) (*U, error)

Reduce computes a value from all the items in the iterator. fn takes two arguments: an accumulator, and an item from the iterator. After all items have been processed, the accumulator is returned. Returns nil, error on collection error.

Types

type Adaptor

type Adaptor[T, Ctx any] struct {
	// contains filtered or unexported fields
}

func NewAdaptor

func NewAdaptor[T, Ctx any](ctx Ctx, pred AdaptorFunc[T, Ctx]) *Adaptor[T, Ctx]

func (*Adaptor[T, Ctx]) Next

func (a *Adaptor[T, Ctx]) Next() (*T, error)

type AdaptorFunc

type AdaptorFunc[T, Ctx any] func(*Ctx) (*T, error)

type Enumerate

type Enumerate[T any] struct {
	// contains filtered or unexported fields
}

Enumerate is an iterator that provides the index of the current item being iterated over.

func NewEnumerate

func NewEnumerate[T any](iter Iterator[T]) *Enumerate[T]

NewEnumerate constructs a new Enumerate over an iterator.

func (*Enumerate[T]) Next

func (e *Enumerate[T]) Next() (*EnumerateItem[T], error)

Next returns the next item in the Enumerate. Returns item, EnumerateError on collection error. This method implements the Iterator interface.

type EnumerateError

type EnumerateError struct {
	// contains filtered or unexported fields
}

EnumerateError is the error type associated with an Enumerate.

func (EnumerateError) Error

func (e EnumerateError) Error() string

Error returns the error message of an EnumerateError.

type EnumerateItem

type EnumerateItem[T any] struct {
	Item  T
	Index int
}

EnumerateItem is the item over which an Enumerate iterates.

type Fill

type Fill[T any] interface {
	Fill(int, T)
}

type Filter

type Filter[T any] struct {
	// contains filtered or unexported fields
}

Filter transforms an iterator by filtering out items based on a predicate function.

func NewFilter

func NewFilter[T any](iter Iterator[T], pred FilterFunc[T]) *Filter[T]

NewFilter constructs a new Filter over an iterator, using pred.

func (*Filter[T]) Next

func (f *Filter[T]) Next() (*T, error)

Next returns the next item in the Filter. Returns nil, error on collection error.

type FilterFunc

type FilterFunc[T any] func(*T) bool

FilterPredicate is a function that returns true if the item should be included in the resulting iterator.

type FromIterator

type FromIterator[T any] interface {
	gollections.Collect[T]
	FromIterator(Iterator[T]) error
}

type Generate

type Generate[T any] interface {
	Generate(int, func() T)
}

type IntoIterator

type IntoIterator[T any] interface {
	IntoIterator() Iterator[T]
}

type Iterable

type Iterable[T any] interface {
	IntoIterator[T]
	FromIterator[T]
}

type Iterator

type Iterator[T any] interface {
	Next() (*T, error)
}

Iterator is an iterface for iterating over collections

type Map

type Map[T, U any] struct {
	// contains filtered or unexported fields
}

Map transforms an iterator over one type into an iterator over another type, converting each item via pred. Returns Iterator[U], error on collection error.

func NewMap

func NewMap[T, U any](iter Iterator[T], pred MapFunc[T, U]) *Map[T, U]

func (*Map[T, U]) Next

func (m *Map[T, U]) Next() (*U, error)

type MapFunc

type MapFunc[T, U any] func(*T) U

type Reverse

type Reverse[T any] interface {
	Prev() (*T, error)
}

type Zip

type Zip[T, U any] struct {
	// contains filtered or unexported fields
}

Zip combines two iterators of potentially different item types.

func NewZip

func NewZip[T any, U any](a Iterator[T], b Iterator[U]) *Zip[T, U]

NewZip constructs a new Zip iterator.

func (*Zip[T, U]) Next

func (z *Zip[T, U]) Next() (*ZipItem[T, U], error)

Next returns the next items from the zipped iterators. This method implements the Iterator interface. Returns item, ZipError on collection error.

type ZipError

type ZipError struct {
	// contains filtered or unexported fields
}

ZipError combines errors from both zipped iterators.

func (ZipError) Error

func (z ZipError) Error() string

Error returns an error message combining errors from both zipped iterators.

type ZipItem

type ZipItem[T, U any] struct {
	A *T
	B *U
}

ZipItem is the type of item a Zip iterates over.

Jump to

Keyboard shortcuts

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