iter

package module
v0.0.0-...-3fa9948 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any, I Iterator[T], P ~func(T) bool](it I, predicate P) bool

All tests if every element of the iterator matches a predicate.

It takes a predicate that returns true or false. It applies this predicate to each element of the iterator, and if they all return true, then so does All. If any of them return false, it returns false.

It will stop processing as soon as it finds a false, given that no matter what else happens, the result will also be false.

An empty iterator returns true.

func Any

func Any[T any, I Iterator[T], P ~func(T) bool](it I, predicate P) bool

Any tests if any element of the iterator matches a predicate.

It takes a predicate that returns true or false. It applies this closure to each element of the iterator, and if any of them return true, then so does Any. If they all return false, it returns false.

It will stop processing as soon as it finds a true, given that no matter what else happens, the result will also be true.

An empty iterator returns false.

func Collect

func Collect[T any, I Iterator[T]](it I, n int) []T

Collect transforms up to n values of an iterator into a slice.

func CollectAll

func CollectAll[T any, I Iterator[T]](it I) []T

CollectAll transforms an iterator into a slice.

func CollectAllInto

func CollectAllInto[T any, I Iterator[T], S ~[]T](it I, result S) S

CollectAllInto gets all values from an iterator and appends them to a given slice returning the same or a new slice.

func CollectInto

func CollectInto[T any, I Iterator[T], S ~[]T](it I, n int, result S) S

CollectInto gets up to N values from an iterator and appends them to a given slice returning the same or a new slice.

func Find

func Find[T any, I Iterator[T], P ~func(T) bool](it I, predicate P) (T, bool)

Find searches for an element of an iterator that satisfies a predicate.

It takes a predicate that returns true or false and applies it to each element of the iterator, and if any of them return true, then Find returns the element and true. If they all return false, it returns zero value and false.

It will stop processing as soon as the predicate returns true.

func Fold

func Fold[T, R any, I Iterator[T], F ~func(R, T) R](it I, initial R, f F) R

Fold takes initial value as the result value and then applies function F to the result value and and next value from the provided iterator and updates the result value.

func ForEach

func ForEach[T any, I Iterator[T], F ~func(T)](it I, fn F)

ForEach consumes the iterator applying fn to each produced value.

func IsZeroValue

func IsZeroValue[T comparable](value T) bool

IsZeroValue returns true if the value is zero initialized.

func Max

func Max[T constraints.Ordered, I Iterator[T]](it I) optional.Value[T]

Max returns maximum value from the iterator if there are any.

func MaxBy

func MaxBy[T any, I Iterator[T], L ~func(T, T) bool](it I, less L) optional.Value[T]

MaxBy returns maximum value from the iterator if there are any. Comparison of two values is performed by the provided less function.

func MaxValue

func MaxValue[T constraints.Ordered](left, right T) T

MaxValue returns maximum value of two values.

func Min

func Min[T constraints.Ordered, I Iterator[T]](it I) optional.Value[T]

Min returns minimum value from the iterator if there are any.

func MinBy

func MinBy[T any, I Iterator[T], L ~func(T, T) bool](it I, less L) optional.Value[T]

MinBy returns minimum value from the iterator if there are any. Comparison of two values is performed by the provided less function.

func MinValue

func MinValue[T constraints.Ordered](left, right T) T

MinValue returns minimum value of two values.

func Reduce

func Reduce[T any, I Iterator[T], F ~func(T, T) T](it I, f F) optional.Value[T]

Reduce reduces iterator using provided function f.

func ZeroValue

func ZeroValue[T any]() T

ZeroValue returns a zero-initialized value of type T.

Types

type CollectorInto

type CollectorInto[T any] interface {
	CollectInto(int, []T) []T
	CollectAllInto([]T) []T
}

CollectorInto allows collecting items from an iterator in a slice.

type Discarder

type Discarder interface {
	Discard(Size) Size
	DiscardAll() Size
}

Discarder allows dropping items from an iterator.

type It

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

It is an iterator wrapper that enables shortcuts for composing new iterators.

func Chain

func Chain[T any](its ...Iterator[T]) It[T]

Chain returns an iterator adapter that concatenates the given iterators into a flat iterator of continuous values.

func Empty

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

Empty returns an iterator that has no values.

func Enumerate

func Enumerate[T any, I Iterator[T]](it I) It[tuple.T2[Size, T]]

Enumerate returns an iterator adapter that transform each value into a tuple consisting of its index and the value itself.

func Filter

func Filter[T any, I Iterator[T], P ~func(T) bool](it I, predicate P) It[T]

Filter returns an iterator adapter that filters each value provided by the underlying iterator using the given predicate.

func Flatten

func Flatten[T any, I Iterator[Iterator[T]]](it I) It[T]

Flatten returns an iterator of values made from iterator of iterators of values.

func Func

func Func[T any, F ~func() (T, bool)](fn F) It[T]

Func returns an iterator that calls the provided function to get next value.

func New

func New[T any](i Iterator[T]) It[T]

New construct an It that is an iterator wrapper that enables shortcuts for composing new iterators.

func Sequence

func Sequence[T constraints.Integer](begin, end T) It[T]

Sequence returns an iterator that produces consecutive integers from begin to end, end is not included.

func Slice

func Slice[T any](values []T) It[T]

Slice returns an iterator that iterates over elements in a slice.

func Take

func Take[T any, I Iterator[T]](it I, n Size) It[T]

Take returns an iterator that produces up to (but no more than) n values from the input iterator.

func Transform

func Transform[T, R any, I Iterator[T], F ~func(T) R](it I, fn F) It[R]

Transform returns an iterator adapter that transforms each value provided by the underlying iterator using fn.

func Zip

func Zip[T, U any, I1 Iterator[T], I2 Iterator[U]](i1 I1, i2 I2) It[tuple.T2[T, U]]

Zip returns an iterator adapter that produces pairs as a tuple of consecutive values from two iterators. In case one of the iterators has next value but other doesn't, Next returns no value. The iterators provided to Zip should not be used anywhere because they may have unpredictable state after usage by Zip due to caching.

func (It[T]) All

func (i It[T]) All(predicate func(T) bool) bool

func (It[T]) Any

func (i It[T]) Any(predicate func(T) bool) bool

func (It[T]) Chain

func (i It[T]) Chain(other ...Iterator[T]) It[T]

func (It[T]) Collect

func (i It[T]) Collect(n int) []T

func (It[T]) CollectAll

func (i It[T]) CollectAll() []T

func (It[T]) CollectAllInto

func (i It[T]) CollectAllInto(result []T) []T

func (It[T]) CollectInto

func (i It[T]) CollectInto(n int, result []T) []T

func (It[T]) Discard

func (i It[T]) Discard(n Size) Size

func (It[T]) DiscardAll

func (i It[T]) DiscardAll() Size

func (It[T]) Filter

func (i It[T]) Filter(predicate func(T) bool) It[T]

func (It[T]) Find

func (i It[T]) Find(predicate func(T) bool) (T, bool)

func (It[T]) Iter

func (i It[T]) Iter() Iterator[T]

func (It[T]) MaxBy

func (i It[T]) MaxBy(less func(T, T) bool) optional.Value[T]

func (It[T]) MinBy

func (i It[T]) MinBy(less func(T, T) bool) optional.Value[T]

func (It[T]) Next

func (i It[T]) Next() (T, bool)

func (It[T]) Reduce

func (i It[T]) Reduce(f func(T, T) T) optional.Value[T]

func (It[T]) SizeHint

func (i It[T]) SizeHint() (Size, bool)

func (It[T]) Take

func (i It[T]) Take(n Size) It[T]

type Iterable

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

Iterable allows getting an iterator over some items.

type Iterator

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

Iterator allows iteration over an abstract stream of items.

type LessBuilder

type LessBuilder[T any, L ~func(T, T) bool] struct {
	// contains filtered or unexported fields
}

LessBuilder allows to compose various comparison functions based on the stored less function.

func UsingLess

func UsingLess[T any, L ~func(T, T) bool](less L) LessBuilder[T, L]

UsingLess returns a build of comparison functions based on the provided less function.

func (LessBuilder[T, L]) Max

func (b LessBuilder[T, L]) Max() func(T, T) T

Max returns a function that will return a maximum of two values comparing them by the stored less function.

func (LessBuilder[T, L]) Min

func (b LessBuilder[T, L]) Min() func(T, T) T

Min returns a function that will return a minimum of two values comparing them by the stored less function.

type Size

type Size = uint64

Size is a number of items in an iterator or an index of item in iterator.

func Discard

func Discard[T any, I Iterator[T]](it I, n Size) Size

Discard consumes up to n elements from an iterator returning the number of consumed elements.

func DiscardAll

func DiscardAll[T any, I Iterator[T]](it I) Size

DiscardAll consumes all elements from an iterator returning the number of consumed elements.

func SizeHint

func SizeHint[T any, I Iterator[T]](it I) (Size, bool)

SizeHint returns estimated number of items left in the iterator if known.

type SizeHinter

type SizeHinter interface {
	SizeHint() (Size, bool)
}

SizeHinter allows getting estimated remaining number of items in an iterator.

Jump to

Keyboard shortcuts

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