iters

package
v0.0.0-...-18275cc Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package iters defines the general iterator interface and provides different operations on top of them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Advance

func Advance[T any](it Iterator[T], n uint)

Advance advances an iterator n steps. Advance stops at any point where the given iterator doesn't have a next element.

func Equal

func Equal[T comparable](it1, it2 Iterator[T]) bool

Equal determines if the elements of two iterators are equal. It returns true if all elements are equal and both iterators have the same number of elements. If the first iterator has l1 elements and the second iterator has l2 elements, Equal advances each iterator min(l1, l2) steps.

func EqualFunc

func EqualFunc[T1 any, T2 any](it1 Iterator[T1], it2 Iterator[T2], eq gcl.EqualFn[T1, T2]) bool

EqualFunc works the same as Equal, but it uses the function eq for element comparison.

func Find

func Find[T any](it Iterator[T], pred func(T) bool) (t T, ok bool)

Find returns the first element in an iterator that satisfies pred. The returned boolean value indicates if such an element exists. If a satisfying element exists, the given iterator it advances the first satisfying element by one step, otherwise Find moves the iterator it to its end.

func Fold

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

Fold applies a function of two arguments cumulatively to the items of the given iterator from the beginning to the end. Fold gives an initial value and start its operation by using the initial value. Fold moves the given iterator it to its end such that after a Fold call it.HasNext() will be false.

func ForEach

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

ForEach calls a function on each element of an iterator. ForEach moves the given iterator it to its end such that after a ForEach call it.HasNext() will be false.

func Max

func Max[T constraints.Ordered](it Iterator[T]) (max T)

Max returns the maximum element in an iterator of any ordered type. Max moves the given iterator it to its end such that after a Max call it.HasNext() will be false.

func MaxFunc

func MaxFunc[T any](it Iterator[T], less gcl.LessFn[T]) (max T)

MaxFunc returns the maximum element in an iterator and uses the given less function for comparison. MaxFunc moves the given iterator it to its end such that after a MaxFunc call it.HasNext() will be false.

func Min

func Min[T constraints.Ordered](it Iterator[T]) (min T)

Min returns the minimum element in an iterator of any ordered type. Min moves the given iterator it to its end such that after a Min call it.HasNext() will be false.

func MinFunc

func MinFunc[T constraints.Ordered](it Iterator[T], less gcl.LessFn[T]) (min T)

MinFunc returns the minimum element in an iterator and uses the given less function for comparison. MinFunc moves the given iterator it to its end such that after a MinFunc call it.HasNext() will be false.

func Reduce

func Reduce[T any](it Iterator[T], fn func(T, T) T) (acc T)

Reduce applies a function of two arguments cumulatively to the items of the given iterator from the beginning to the end. Reduce moves the given iterator it to its end such that after a Reduce call, it.HasNext() will be false.

func Sum

func Sum[T gcl.Number](it Iterator[T]) (sum T)

Sum returns sum of the elements in an iterator of any numeric type. Sum moves the given iterator it to its end such that after a Sum call it.HasNext() will be false.

Types

type Iterator

type Iterator[T any] interface {
	// HasNext tests whether the iterator can advance.
	HasNext() bool

	// Next advances the iterator and returns the next value in iteration.
	Next() T
}

Iterator defines the general iterator interface. Iterator makes it possible to iterate over collections.

func Filter

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

Filter filters elements of an iterator that satisfy the pred. Filter returns an iterator over the filtered elements. Filter moves the given iterator it to its end such that after a Filter call it.HasNext() will be false. Filter is lazy, in a way that if you don't consume the returned iterator nothing will happen.

func Map

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

Map applies the function fn on elements the given iterator it and returns a new iterator over the mapped values. Map moves the given iterator it to its end such that after a Map call it.HasNext() will be false. Map is lazy, in a way that if you don't consume the returned iterator nothing will happen.

func Zip

func Zip[T1 any, T2 any](it1 Iterator[T1], it2 Iterator[T2]) Iterator[gcl.Zipped[T1, T2]]

Zip zips the two given iterators and returns a single iterator over gcl.Zipped values.

Jump to

Keyboard shortcuts

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