iterator

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIteratorEmpty = errors.New("iterator is empty")

Functions

func Split added in v0.5.0

func Split[T any](iter Iterator[T], predicate func(T) bool) (Iterator[T], Iterator[T])

Split returns two iterators that split the items of the given iterator using the given predicate. The first iterator will only yield items for which the predicate returns true, while the second iterator will only yield items for which the predicate returns false.

func Tee added in v0.5.0

func Tee[T any](it Iterator[T]) (Iterator[T], Iterator[T])

Tee returns two iterators that iterate over the same underlying iterator. Both iterators can consume the items independently.

Types

type Iterator

type Iterator[T any] interface {
	// HasNext returns true if the iteration has more elements.
	HasNext() bool

	// Next returns the next element in the iteration. If there are no more
	// elements, it panics with ErrIteratorEmpty.
	Next() T

	// Collect returns a slice containing all the remaining elements in the
	Collect() []T
}

Iterator is an interface that represents an iterator over a collection of elements of type T.

func Cycle added in v0.5.0

func Cycle[T any](items ...T) Iterator[T]

Cycle returns an iterator that cycles through the given items. The returned iterator will yield the items in the same order as they were given, and will repeat the cycle indefinitely.

func Filter added in v0.5.0

func Filter[T any](iter Iterator[T], predicate func(T) bool) Iterator[T]

Filter returns an iterator that filters the items of the given iterator using the given predicate. The returned iterator will only yield items for which the predicate returns true.

func From added in v0.5.0

func From[T any](source ...T) Iterator[T]

From returns an iterator over the given arguments.

func FromSlice

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

FromSlice returns an iterator over the given slice.

func Map added in v0.5.0

func Map[From any, To any](collection Iterator[From], mapper func(it From) To) Iterator[To]

Map calls the provided mapper function once for each element in the given iterator, in order, and constructs a new iterator from the results.

Jump to

Keyboard shortcuts

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