iterator

package
v0.0.0-...-020adb6 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

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

Find returns a first element in `it` that satisfies the given predicate `fn`. It returns `false` as a second return value if no elements are found.

func FindElem

func FindElem[T any](eq cmp.Eq[T]) func(it Iterator[T], e T) (T, bool)

FindElem returns a first element in `it` that equals to `e` in the sense of given `Eq`. It returns `false` as a second return value if no elements are found.

func FindElemIndex

func FindElemIndex[T any](eq cmp.Eq[T]) func(it Iterator[T], e T) int

FindElemIndex returns a first index of an element in `it` that equals to `e` in the sense of given `Eq`. It returns negative value if no elements are found.

func FindIndex

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

FindIndex returns a first index of an element in `it` that satisfies the given predicate `fn`. It returns negative value if no elements are found.

func Fold

func Fold[T, U any](init T, it Iterator[U], fn func(T, U) T) T

Fold accumulates every element in Iterator by applying fn.

func ForEach

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

ForEach applies fn to each element in it.

func LiftM

func LiftM[T, U any](fn func(T) U) func(Iterator[T]) Iterator[U]

LiftM promotes a function fn to a monad.

func Max

func Max[T any](ord cmp.Ord[T]) func(it Iterator[T]) (T, bool)

Max returns the largest element with respect to the given Ord. It returns `<zero value>, false` if the iterator is empty.

func MaxBy

func MaxBy[T any](it Iterator[T], less func(T, T) bool) (T, bool)

MaxBy returns the largest element with respect to the given function. It returns `<zero value>, false` if the iterator is empty.

func Min

func Min[T any](ord cmp.Ord[T]) func(it Iterator[T]) (T, bool)

Min returns the smallest element with respect to the given Ord. It returns `<zero value>, false` if the iterator is empty.

func MinBy

func MinBy[T any](it Iterator[T], less func(T, T) bool) (T, bool)

MinBy returns the smallest element with respect to the given function. It returns `<zero value>, false` if the iterator is empty.

func Sum

func Sum[T any](m algebra.Monoid[T]) func(it Iterator[T]) T

Sum sums up all values in `it`. It returns `Empty()` when `it` is empty.

func SumWithInit

func SumWithInit[T any](s algebra.Semigroup[T]) func(init T, it Iterator[T]) T

SumWithInit sums up `init` and all values in `it`.

Types

type Iterator

type Iterator[T any] interface {
	// Next returns the next element in this Iterable and advances its state.
	// The second return value is false if and only if there are no elements to return.
	Next() (T, bool)
}

Iterable iterates over some set of elements.

func AndThen

func AndThen[T, U any](x Iterator[T], fn func(T) Iterator[U]) Iterator[U]

AndThen composes a monadic value x and action fn.

func Filter

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

Filter returns an Iterator that only returns elements that satisfies given predicate.

func FlatMap

func FlatMap[T, U any](it Iterator[T], fn func(T) Iterator[U]) Iterator[U]

FlatMap applies fn to each element in it and then joins them.

func Map

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

Map returns an iterator that applies fn to each element of it.

func Pure

func Pure[T any](x T) Iterator[T]

Pure returns an Iterator that contains a single element x.

func Range

func Range[T constraints.Integer](start, end T) Iterator[T]

Range returns an Iterator that returns start, start+1, ..., end-1, end, sequentially. The returned Iterator does not return any valeus if end is smaller than start.

func Take

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

Taks takes the first n elements in `it`.

func Unfold

func Unfold[T, U any](init T, step func(T) (T, U, bool)) Iterator[U]

Unfold returns an Iterator `it` that has an initial state `init` and updating function `step`. On each call to `it.Next()`, it updates its internal state by applying `step` and return the second return value. If the third return value of `step` is `false`, `it.Next()` stops iterating and returns `(<zero value>, false)`.

func Zip

func Zip[T, U any](a Iterator[T], b Iterator[U]) Iterator[pair.Pair[T, U]]

Zip combines two Iterators into one that yields pairs of corresponding elements.

Jump to

Keyboard shortcuts

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