iterator

package
v0.0.0-...-41ddf2c Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](it collections.Iterator[T], p collections.Predicate[T]) bool

All reads value from the given iterator until one of the values causes the given predicate to return false, then false is returned. If the predicated returns true for all values, then true is returned.

func Any

func Any[T any](it collections.Iterator[T], p collections.Predicate[T]) bool

Any reads values from the given iterator until one of the values causes the given predicate to return true, then true is returned. If the predicated returns false for all values, then false is returned.

func Append

func Append[T any](it collections.Iterator[T], tails []T) collections.Iterator[T]

Append creates an iterator with the given value appended to the end of the values.

func AtLeast

func AtLeast[T any](it collections.Iterator[T], min int) bool

AtLeast reads only enough values from the given iterator to determine if there is at least the given number of values exists.

func AtMost

func AtMost[T any](it collections.Iterator[T], max int) bool

AtMost reads all the values from the given iterator to determine if there is at most the given number of values exists.

func Cast

func Cast[TIn, TOut any](it collections.Iterator[TIn]) collections.Iterator[TOut]

Cast casts the given type into the given out type If a cast isn't possible then zero is returned.

func Chunk

func Chunk[T any](it collections.Iterator[T], size int) collections.Iterator[[]T]

Chunk creates an iterator which has the values grouped into chunks of the given size. Each chunk is the given size of values put into a slice. The chunks have already been copied. The last chunk may not be the given size, it may be the remaining values.

func Concat

func Concat[T any](its []collections.Iterator[T]) collections.Iterator[T]

Concat concatenates the given iterators into one iterator.

func CopyToSlice

func CopyToSlice[T any](it collections.Iterator[T], s []T)

CopyToSlice reads values out of the given iterator adds them to the given slice. This will stop when either values run out or there is no more room in the slice.

func Count

func Count[T any](it collections.Iterator[T]) int

Count reads all the values from the given iterator and returns how many values were in the iterator.

func DoUntilError

func DoUntilError[T any](it collections.Iterator[T], s collections.Selector[T, error]) error

DoUntilError runs the given function for each value from the given iterator. When an error is returned by the selector, the iteration ends and returns that error. If no error is hit, nil is returned.

func DoUntilNotZero

func DoUntilNotZero[TIn, TOut any](it collections.Iterator[TIn], s collections.Selector[TIn, TOut]) TOut

DoUntilNotZero runs the given function for each value from the given iterator. When a non-zero value is returned by the selector, the iteration ends and returns that non-zero value. If no non-zero value is hit, a zero value is returned.

func Empty

func Empty[T any](it collections.Iterator[T]) bool

Empty attempts to read the next value off the given iterator. If a value exists, the iterator isn't empty, otherwise false.

func Equal

func Equal[T any](it1, it2 collections.Iterator[T]) bool

Equal determines if the two iterators contain the same values.

func Expand

func Expand[TIn, TOut any, TEnum collections.Iterable[TOut]](
	it collections.Iterator[TIn],
	expander collections.Selector[TIn, TEnum],
) collections.Iterator[TOut]

Expand creates an iterator which iterates all the values from all the iterators selected from the values in the given iterator.

func First

func First[T any](it collections.Iterator[T]) (T, bool)

First reads one value off the given iterator, if one exists, otherwise the zero value is returned.

func Foreach

func Foreach[T any](it collections.Iterator[T], m func(value T))

Foreach runs the given function for each values from the given iterator.

func Indexed

Indexed returns an iterator that returns a tuple containing the value from the given iterator and an index of the value starting with zero.

func Intersection

func Intersection[T comparable](left, right collections.Iterator[T]) collections.Iterator[T]

Intersection creates an iterator that returns only the values which exists in both iterators.

The right iterator takes precedence over the result such that it determines the order and if there are repeats in the result.

func Interweave

func Interweave[T any](its []collections.Iterator[T]) collections.Iterator[T]

Interweave will pull values from each iterator, one at a time, and return them in the cycling order as an iterator. When an iterator runs out the remaining will interweave until all are empty.

func IsUnique

func IsUnique[T comparable](it collections.Iterator[T]) bool

IsUnique determines if there are only unique items in the iterator. Returns false if there are duplicate values in the iterator.

func Iterate

func Iterate[T any](values ...T) collections.Iterator[T]

Iterate will iterate the given values.

func Last

func Last[T any](it collections.Iterator[T]) (T, bool)

Last reads all the values off the given iterator and returns the last value. Returns zero and false if there were no values in the iterator.

func Merge

func Merge[T any](it collections.Iterator[T], merger collections.Reducer[T, T]) T

Merge reads all the values from the given iterator together. The merge method is called with the prior returned value from the previous call. The first value is used as the prior value with the second value in the merger. The last returned value from merge is returned, the first value if there is only one value, or the zero value if no values.

func New

func New[T any](fetcher Fetcher[T]) collections.Iterator[T]

New creates a new iterator for stepping through values using a fetcher. As soon as the fetcher returns false this iterator will stop.

func OfType

func OfType[TIn, TOut any](it collections.Iterator[TIn]) collections.Iterator[TOut]

OfType returns only the values of the given out type.

func Range

func Range[T utils.NumConstraint](start T, count int) collections.Iterator[T]

Range will iterate from the given count of values. The range monotonically increments by one from the given start value.

func Reduce

func Reduce[TIn, TOut any](it collections.Iterator[TIn], init TOut, reducer collections.Reducer[TIn, TOut]) TOut

Reduce reads all the values from the given iterator. The reduce method is called with the prior returned value from the previous call. The first call is given the initial value. The last returned value from reduce is returned. or init if no values.

func Repeat

func Repeat[T any](value T, count int) collections.Iterator[T]

Repeat will iterate the same value the given count.

func Replace

func Replace[T any](it collections.Iterator[T], replacer collections.Selector[T, T]) collections.Iterator[T]

Replace creates a new iterator which checks each value using the given replacer. This gives an opportunity for values to be replaced with a different value or returned unchanged whilst iterating over the values from the given iterator.

func Reverse

func Reverse[T any](it collections.Iterator[T]) collections.Iterator[T]

Reverse reads all the values from the given iterator and returns them in reverse order. No values are read from the given iterator, until a value is read from the new iterator.

Try to maximize the number of values used after a reverse, since it is a waste of time and memory to reverse a large iteration if only the first three values (the last three in the iteration) are used. Reducing down to a smaller iterator where all the values are needed in reverse is better than reversing unneeded values.

func Select

func Select[TIn, TOut any](
	it collections.Iterator[TIn],
	selector collections.Selector[TIn, TOut],
) collections.Iterator[TOut]

Select changes one iterator type into another by converting each value. Typically this is used to select one value out of a value.

func Single

func Single[T any](it collections.Iterator[T]) (T, bool)

Single reads two values off the given iterator. If there is only one, then it is returned.

func Skip

func Skip[T any](it collections.Iterator[T], count int) collections.Iterator[T]

Skip creates a new iterator which skips over the first given count number of values from the given iterator before iterating the remaining values. The skipped values aren't read until the first value read from the returned iterator.

func SkipWhile

func SkipWhile[T any](it collections.Iterator[T], p collections.Predicate[T]) collections.Iterator[T]

SkipWhile creates a new iterator which skips over values until the given predicate returns false. The values from the given iterator are returned after and including the first false from the predicate. The skipped values aren't read until the first value read from the returned iterator.

func SlidingWindow

func SlidingWindow[TIn, TOut any](
	it collections.Iterator[TIn],
	size, stride int,
	window collections.Window[TIn, TOut],
) collections.Iterator[TOut]

SlidingWindow creates an iterator which handles a sliding window over the values from the given iterator.

The sliding widow is always the specified size. The size must be greater than zero. The given stride is how far to advance the window, it must be [1..size]. The same slice is reused for the window so modifying it could cause problems and it should be copied if the window is kept.

func Sort

func Sort[T any](it collections.Iterator[T], comparer ...utils.Comparer[T]) collections.Iterator[T]

Sort iterates the values from the given iterator in sorted order. The values are sorted by the given comparer function or the default comparer. This can take an optional comparer to override the default or if this type doesn't have a default comparer.

func SortInterweave

func SortInterweave[T any](left, right collections.Iterator[T], comparer ...utils.Comparer[T]) collections.Iterator[T]

SortInterweave creates an iterator that is the two given iterators interwoven such that both lists keep their order but lowest value from each list is used first. If the two iterators are sorted, this will effectively merge sort the values. This can take an optional comparer to override the default or if this type doesn't have a default comparer.

func Sorted

func Sorted[T any](it collections.Iterator[T], comparer ...utils.Comparer[T]) bool

Sorted returns true if the given values in the iterator are sorted based on the given comparer or the default comparer. This can take an optional comparer to override the default or if this type doesn't have a default comparer.

func StartsWith

func StartsWith[T any](it, prefix collections.Iterator[T]) bool

StartsWith determines if the first iterator starts with the given prefix.

func StepsUntil

func StepsUntil[T any](it collections.Iterator[T], p collections.Predicate[T]) int

StepsUntil determines the number of values in the iterator are read until a value satisfies the given predicate.

The count will not include the value which satisfied the predicate such that if the first value satisfies the predicate then this will return zero. If no value satisfies the predicate then -1 is returned.

func Stride

func Stride[T utils.NumConstraint](start, step T, count int) collections.Iterator[T]

Stride will iterate from the given count of values. This will add the given step between each number, starting from the given start value.

func Subtract

func Subtract[T comparable](left, right collections.Iterator[T]) collections.Iterator[T]

Subtract creates an iterator that returns only the values which exists in the right iterator but not in the left. This will subtract the left set from the right set.

The right iterator takes precedence over the result such that it determines the order and if there are repeats in the result.

func Sum

func Sum[T utils.NumConstraint](it collections.Iterator[T]) (T, int)

Sum gets the sum of all value in the given iterator and the number of values that were summed.

func Take

func Take[T any](it collections.Iterator[T], count int) collections.Iterator[T]

Take creates a new iterator which only takes the given count of values form the given iterator before stopping iteration.

func TakeWhile

func TakeWhile[T any](it collections.Iterator[T], p collections.Predicate[T]) collections.Iterator[T]

TakeWhile creates a new iterator which only takes values until the given predicate returns false. The values from the given iterator are returned until and excluding the first false from the predicate.

func ToSlice

func ToSlice[T any](it collections.Iterator[T]) []T

ToSlice reads all the values out of the given iterator and returns them as a slice.

func Unique

Unique creates an iterator that returns only the unique items in the iterator.

func Where

Where creates an iterator which reads from the given iterator but only returns values which the given predicate returns true for.

func WhereNot

func WhereNot[T any](it collections.Iterator[T], p collections.Predicate[T]) collections.Iterator[T]

WhereNot creates an iterator which reads from the given iterator but only returns values which the given predicate returns false for.

func Zip

func Zip[TFirst, TSecond, TOut any](
	firsts collections.Iterator[TFirst],
	seconds collections.Iterator[TSecond],
	combiner collections.Combiner[TFirst, TSecond, TOut],
) collections.Iterator[TOut]

Zip merges two iterators together while both iterators have values and returns an iterator with a tuple containing values from both iterators.

Types

type Fetcher

type Fetcher[T any] func() (T, bool)

Fetcher is the source of values which can be used in iterators.

Each time it is called it can return a different value. Returns true if a new value was fetched and false if there are no new values. Once false is returned it is expected to always return false from then on.

Jump to

Keyboard shortcuts

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