iter

package module
v0.0.0-...-591e026 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2022 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count[T any](i Iterator[T]) int

Count returns the number of elements in the iterator. This consumes the iterator.

func Fold

func Fold[Acc, T any](i Iterator[T], acc Acc, step func(Acc, T) Acc) Acc

Fold accumulates a value by applying the step function to each element.

func Last

func Last[T any](i Iterator[T]) (t T, ok bool)

Last returns the last value of this iterator, if any exists. If empty, Last returns zero and false.

func Max

func Max[T constraints.Ordered](i Iterator[T]) (T, bool)

Max returns the maximum value and true if the collection was not empty, and zero and false otherwise.

func MaxBy

func MaxBy[T any](i Iterator[T], less func(a, b T) bool) (T, bool)

MaxBy returns the maximum value using the given comparison function and true if the collection was not empty, and zero and false otherwise.

func Min

func Min[T constraints.Ordered](i Iterator[T]) (T, bool)

Min returns the minimum value and true if the collection was not empty, and zero and false otherwise.

func MinBy

func MinBy[T any](i Iterator[T], less func(a, b T) bool) (T, bool)

MinBy returns the minimum value using the given comparison function and true if the collection was not empty, and zero and false otherwise.

func Reduce

func Reduce[T any](i Iterator[T], step func(T, T) T) (acc T, ok bool)

Reduce accumulates a value by applying the step function to the accumulated value and the next element. This is a specialized version of Fold, and returns a boolean value which is false if the collection was empty.

func Sum

Sum returns the sum of elements in the iterator. This consumes the iterator.

Types

type ChanIterator

type ChanIterator[T any] chan T

ChanIterator is an iterator backed by a channel.

func (ChanIterator[T]) Advance

func (c ChanIterator[T]) Advance(n int) int

func (ChanIterator[T]) Collect

func (c ChanIterator[T]) Collect() []T

func (ChanIterator[T]) EstimatedRemaining

func (c ChanIterator[T]) EstimatedRemaining() int

func (ChanIterator[T]) Next

func (c ChanIterator[T]) Next() (T, bool)

type Iterable

type Iterable[T any] interface {
	Iter() Iterator[T]
}

Iterable represents an object which can be iterated over.

type Iterator

type Iterator[T any] interface {
	// Next returns the next element, or a zero element and false if there are no more elements
	Next() (T, bool)

	// Advance moves the iterator forward by n elements, returning how much it was actually able to progress.
	Advance(n int) int

	// EstimatedRemaining returns a lower bound on the number of elements remaining,
	// i.e. there are at least Remaining() elements remaining.
	// WARNING: This is a *lower* bound! There may be more elements, even if this returns zero!
	EstimatedRemaining() int

	// Collect returns a slice containing the elements of the iterator.
	Collect() []T
}

Iterator represents a one-time one-by-one view of elements through some internal state.

func Empty

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

Empty creates an iterator which contains no values.

func Filter

func Filter[T any](i Iterator[T], filter func(T) bool) Iterator[T]

Filter wraps an iterator, skipping elements for which filter returns false.

func FromChannel

func FromChannel[T any](c chan T) Iterator[T]

FromChannel creates a new iterator which iterates over the channel `c`

func FromSlice

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

FromSlice creates a new SliceIterator from a slice.

func Map

func Map[T, U any](i Iterator[T], mapper func(T) U) Iterator[U]

Map applies a transformation to all elements and yields those transformed elements.

type SliceIterator

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

SliceIterator enables iteration over slices.

func (*SliceIterator[T]) Advance

func (s *SliceIterator[T]) Advance(n int) int

func (*SliceIterator[T]) Collect

func (s *SliceIterator[T]) Collect() []T

func (*SliceIterator[T]) EstimatedRemaining

func (s *SliceIterator[T]) EstimatedRemaining() int

func (*SliceIterator[T]) Next

func (s *SliceIterator[T]) Next() (out T, ok bool)

func (*SliceIterator[T]) Reset

func (s *SliceIterator[T]) Reset()

Jump to

Keyboard shortcuts

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