Documentation ¶
Index ¶
- Variables
- func Async[T any, V any](iterator Iterator[T], fn func(Iterator[T]) (V, error)) <-chan ValErr[V]
- func Concat[T ~string](iter Iterator[T], sep T) (T, error)
- func ContainsAll[T comparable](iter Iterator[T], values ...T) (bool, error)
- func ContainsAny[T comparable](iter Iterator[T], values ...T) (bool, error)
- func ContainsFunc[T any](iter Iterator[T], fn func(int, T) (bool, error)) (bool, error)
- func Equal[T comparable](iters ...Iterator[T]) (bool, error)
- func Fold[T any, S any](iter Iterator[T], start S, fn func(int, T, S) (S, error)) (S, error)
- func Iterate[T any](iterator Iterator[T], f func(int, T) (bool, error)) (int, error)
- func Len[T any](iter Iterator[T]) (int, error)
- func One[T any](iterator Iterator[T]) (T, error)
- func Reduce[T any](iter Iterator[T], fn func(int, T, T) (T, error)) (T, error)
- func String[T any](iter Iterator[T]) (string, error)
- func Sum[T constraints.Float | constraints.Integer | constraints.Complex](iter Iterator[T]) (T, error)
- func ToChannel[T any](iter Iterator[T], size int) (<-chan ValErr[T], func())
- func ToFunc[T any](iter Iterator[T]) func() (T, bool, error)
- func ToMap[K comparable, V any](iter Iterator[KV[K, V]]) (map[K]V, error)
- func ToSlice[T any](iter Iterator[T]) ([]T, error)
- type Iterator
- func Ascending[T constraints.Float | constraints.Integer](start T, step T) Iterator[T]
- func Assoc[K comparable, V any](keys Iterator[K], values Iterator[V]) Iterator[KV[K, V]]
- func Const[T any](value T) Iterator[T]
- func Descending[T constraints.Float | constraints.Integer](start T, step T) Iterator[T]
- func Distinct[T comparable](iter Iterator[T]) Iterator[T]
- func Duplicates[T comparable](iter Iterator[T]) Iterator[T]
- func Easing[T constraints.Float](n int, fn func(float64) float64) Iterator[T]
- func Empty[T any]() Iterator[T]
- func Fibonacci[T constraints.Float | constraints.Integer]() Iterator[T]
- func Flatten[T any](iter Iterator[Iterator[T]]) Iterator[T]
- func FromChannel[T any](source <-chan T) Iterator[T]
- func FromFunc[T any](next func() (T, bool, error)) Iterator[T]
- func FromMap[K comparable, V any](source map[K]V) Iterator[KV[K, V]]
- func FromSlice[T any](source []T) Iterator[T]
- func FromValErrChannel[T any](source <-chan ValErr[T]) Iterator[T]
- func Group[T comparable](iter Iterator[T]) Iterator[Iterator[T]]
- func Interleave[T any](count int, iters ...Iterator[T]) Iterator[T]
- func InterleaveFunc[T any](fn func(iterIndex int, currRun int, index int, item T) (bool, error), ...) Iterator[T]
- func Join[T any](iters ...Iterator[T]) Iterator[T]
- func Keys[K comparable, V any](iter Iterator[KV[K, V]]) Iterator[K]
- func Merge[T any](iters ...Iterator[T]) Iterator[T]
- func Mirror[T any](iter Iterator[T], count int) []Iterator[T]
- func OnClose[T any](iter Iterator[T], fn func() error) Iterator[T]
- func Once[T any](value T) Iterator[T]
- func Pipe[T any](iter Iterator[T], mods ...Modifier[T, T]) Iterator[T]
- func Random[T constraints.Float | constraints.Integer](max T) Iterator[T]
- func RandomBetween[T constraints.Float | constraints.Integer](min, max T) Iterator[T]
- func RandomBetweenFunc[T constraints.Float | constraints.Integer](min, max T, fn func() float64) Iterator[T]
- func RandomFunc[T constraints.Float | constraints.Integer](max T, fn func() float64) Iterator[T]
- func Range[T constraints.Float | constraints.Integer](start T, end T, step T) Iterator[T]
- func Runs[T comparable](iter Iterator[T]) Iterator[Iterator[T]]
- func Sqrt[T constraints.Float | constraints.Integer](iter Iterator[T]) Iterator[T]
- func Strings[T any](iter Iterator[T]) Iterator[string]
- func ToSlices[T any](iter Iterator[Iterator[T]]) Iterator[[]T]
- func Unfold[T any, S any](state S, fn func(int, S) (T, S, bool, error)) Iterator[T]
- func Uniques[T comparable](iter Iterator[T]) Iterator[T]
- func Values[K comparable, V any](iter Iterator[KV[K, V]]) Iterator[V]
- func Zero[T any]() Iterator[T]
- type KV
- type Modifier
- func Add[T constraints.Float | constraints.Integer | ~string](x T) Modifier[T, T]
- func Append[T any](items ...T) Modifier[T, T]
- func AssocKeys[K comparable, V any](keys Iterator[K]) Modifier[V, KV[K, V]]
- func AssocValues[K comparable, V any](values Iterator[V]) Modifier[K, KV[K, V]]
- func Boomerang[T any](times int) Modifier[T, T]
- func Chunk[T any](size int) Modifier[T, Iterator[T]]
- func Clamp[T constraints.Ordered](min T, max T) Modifier[T, T]
- func Cycle[T any](times int) Modifier[T, T]
- func DistinctFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, T]
- func Distribute[T any](buffer int) Modifier[T, Iterator[T]]
- func Div[T constraints.Float | constraints.Integer](x T) Modifier[T, T]
- func DropWhile[T any](pred func(int, T) (bool, error)) Modifier[T, T]
- func DuplicatesFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, T]
- func Echo[T any](times int) Modifier[T, T]
- func EchoFunc[T any](fn func(int, T) (int, error)) Modifier[T, T]
- func Filter[T any](pred func(int, T) (bool, error)) Modifier[T, T]
- func FilterMap[T any, S any](fn func(int, T) (S, bool, error)) Modifier[T, S]
- func FlatMap[T any, S any](fn func(int, int, T) (Iterator[S], error)) Modifier[T, S]
- func GroupFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, Iterator[T]]
- func Inject[T any](index int, in Iterator[T]) Modifier[T, T]
- func InjectFunc[T any](in Iterator[T], fn func(int, T) (bool, error)) Modifier[T, T]
- func Insert[T any](index int, items ...T) Modifier[T, T]
- func InsertFunc[T any](fn func(int, T) (bool, error), items ...T) Modifier[T, T]
- func Interpolate[T constraints.Float | constraints.Integer, S constraints.Float](start1, end1 T, start2, end2 S) Modifier[T, S]
- func JoinLeading[T any](leading Iterator[T]) Modifier[T, T]
- func JoinTrailing[T any](trailing Iterator[T]) Modifier[T, T]
- func Limit[T any](limit int) Modifier[T, T]
- func Map[T any, S any](fn func(int, T) (S, error)) Modifier[T, S]
- func Mod[T constraints.Integer](x T) Modifier[T, T]
- func Mul[T constraints.Float | constraints.Integer](x T) Modifier[T, T]
- func Normalize[T constraints.Float | constraints.Integer, S constraints.Float](min, max T) Modifier[T, S]
- func Nwise[T any](size int) Modifier[T, Iterator[T]]
- func Offset[T any](offset int) Modifier[T, T]
- func Pow[T constraints.Float | constraints.Integer](x T) Modifier[T, T]
- func Prepend[T any](items ...T) Modifier[T, T]
- func Remove[T comparable](rem T) Modifier[T, T]
- func RemoveFunc[T any](fn func(int, T) (bool, error)) Modifier[T, T]
- func Repeat[T any](from int, to int, times int) Modifier[T, T]
- func Replace[T comparable](old T, new T, times int) Modifier[T, T]
- func ReplaceAll[T comparable](old T, new T) Modifier[T, T]
- func RunsFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, Iterator[T]]
- func Samples[T any](population, sample int) Modifier[T, T]
- func SamplesFunc[T any](population, sample int, fn func() float64) Modifier[T, T]
- func Slice[T any](from, to, step int) Modifier[T, T]
- func Splice[T any](from, to int, injected Iterator[T]) Modifier[T, T]
- func Split[T comparable](sep T) Modifier[T, Iterator[T]]
- func SplitFunc[T any](fn func(int, T) (split, inA, inB bool, err error)) Modifier[T, Iterator[T]]
- func SplitLeading[T comparable](sep T) Modifier[T, Iterator[T]]
- func SplitTrailing[T comparable](sep T) Modifier[T, Iterator[T]]
- func Sub[T constraints.Float | constraints.Integer](x T) Modifier[T, T]
- func TakeWhile[T any](pred func(int, T) (bool, error)) Modifier[T, T]
- func UniquesFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, T]
- type ValErr
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoItems = errors.New("iterator: no items in iterator") ErrMultiItems = errors.New("iterator: multiple items in iterator") )
Functions ¶
func Async ¶
Async calls fn with the given iterator in a separate goroutine and returns the result in a ValErr channel.
func ContainsAll ¶
func ContainsAll[T comparable](iter Iterator[T], values ...T) (bool, error)
ContainsAll returns true when it has encountered all the given values in the given iterator.
func ContainsAny ¶
func ContainsAny[T comparable](iter Iterator[T], values ...T) (bool, error)
ContainsAny returns true as soon as it encounters any of the given values in the given iterator.
func ContainsFunc ¶
ContainsFunc calls fn on each item in the given iterator. It stops as soon as fn returns true and reports that.
func Equal ¶
func Equal[T comparable](iters ...Iterator[T]) (bool, error)
Equal reports whether the given iterators are equal
func One ¶
One returns the only item of an iterator. If the iterator has no items then it returns ErrNoItems, and if the iterator has more than one item then it returns ErrMultiItems.
func Sum ¶
func Sum[T constraints.Float | constraints.Integer | constraints.Complex](iter Iterator[T]) (T, error)
Sum all numbers in the iterator
Types ¶
type Iterator ¶
type Iterator[T any] interface { // Next moves the iterator to the next position and reports whether // an item exists Next() bool // Get returns the current item // Multiple calls to Get without calling Next should give the same result Get() (T, error) // Close marks the iterator as done and frees resources Close() error // Err returns the first error encountered in any of the operations of the iterator Err() error }
Iterator defines the methods needed to conform to an iterator supported by this package
func Ascending ¶
func Ascending[T constraints.Float | constraints.Integer](start T, step T) Iterator[T]
Ascending returns an iterator of numbers from start increasing by step
func Descending ¶
func Descending[T constraints.Float | constraints.Integer](start T, step T) Iterator[T]
Descending returns an iterator of numbers from start decreasing by step
func Distinct ¶
func Distinct[T comparable](iter Iterator[T]) Iterator[T]
Distinct is a modifier that skips duplicate items
func Fibonacci ¶
func Fibonacci[T constraints.Float | constraints.Integer]() Iterator[T]
Fibonacci returns an iterator for fibonacci numbers
func Flatten ¶
Flatten is a modifier that applies on iterator of iterators It flattens them into a single iterator
func FromChannel ¶
FromChannel returns an iterator wrapping a channel source
func FromMap ¶
func FromMap[K comparable, V any](source map[K]V) Iterator[KV[K, V]]
FromMap returns an iterator wrapping a map source
func FromValErrChannel ¶
FromValErrChannel returns an iterator wrapping a channel source with items of type ValErr
func Interleave ¶
Interleave returns an iterator that alternates between the given iterators. It will try to take as many as count from each iterator in each round
func InterleaveFunc ¶
func InterleaveFunc[T any](fn func(iterIndex int, currRun int, index int, item T) (bool, error), iters ...Iterator[T]) Iterator[T]
InterleaveFunc returns an iterator that alternates between the given iterators. It switches to the next iterator when fn returns true.
func Keys ¶
func Keys[K comparable, V any](iter Iterator[KV[K, V]]) Iterator[K]
Keys is a modifier that takes a map iterator and returns an iterator for the keys
func Random ¶
func Random[T constraints.Float | constraints.Integer](max T) Iterator[T]
Random returns an iterator for random numbers up to max.
func RandomBetween ¶
func RandomBetween[T constraints.Float | constraints.Integer](min, max T) Iterator[T]
RandomBetween returns an iterator for random numbers from min and up to max.
func RandomBetweenFunc ¶
func RandomBetweenFunc[T constraints.Float | constraints.Integer](min, max T, fn func() float64) Iterator[T]
RandomBetweenFunc returns an iterator for random numbers from min and up to max. It uses fn as a random number generator in the interval [0.0,1.0).
func RandomFunc ¶
func RandomFunc[T constraints.Float | constraints.Integer](max T, fn func() float64) Iterator[T]
RandomFunc returns an iterator for random numbers up to max. It uses fn as a random number generator in the interval [0.0,1.0).
func Range ¶
func Range[T constraints.Float | constraints.Integer](start T, end T, step T) Iterator[T]
Range returns an iterator of numbers from start to end in increments/decrements of step It can include end if it matches a step increment/decrement
func Runs ¶
func Runs[T comparable](iter Iterator[T]) Iterator[Iterator[T]]
Runs is a modifier that splits the iterator into multiple iterators each containing matching consecutive items.
func Sqrt ¶
func Sqrt[T constraints.Float | constraints.Integer](iter Iterator[T]) Iterator[T]
Sqrt is a modifier to get the square root of items.
type KV ¶
type KV[K comparable, V any] struct { Key K Val V }
type Modifier ¶
Modifier applys an operation on the iterator and returns the resultant iterator The given iterator should not be used for anything else after applying the modifier to avoid incorrect results
func Add ¶
func Add[T constraints.Float | constraints.Integer | ~string](x T) Modifier[T, T]
Add returns a modifier to add x to items.
func AssocKeys ¶
func AssocKeys[K comparable, V any](keys Iterator[K]) Modifier[V, KV[K, V]]
AssocKeys returns a modifier that associates the iterator to the given keys
func AssocValues ¶
func AssocValues[K comparable, V any](values Iterator[V]) Modifier[K, KV[K, V]]
AssocValues returns a modifier that associates the iterator to the given values
func Boomerang ¶
Boomerang repeats the iterator for as many times given and alternates between reverse and original order.
func Clamp ¶
func Clamp[T constraints.Ordered](min T, max T) Modifier[T, T]
Clamp returns a modifier to clamps items within min and max inclusively.
func DistinctFunc ¶
func DistinctFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, T]
func Distribute ¶
Distribute returns an iterator of the specified number of iterators. Each iterator contains a subset of the items and all of them can be consumed in parallel.
func Div ¶
func Div[T constraints.Float | constraints.Integer](x T) Modifier[T, T]
Div returns a modifier to divide items by x.
func DropWhile ¶
DropWhile returns a modifier which makes the iterator drop items matching pred and starts with the first item that does not.
func DuplicatesFunc ¶
func DuplicatesFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, T]
DuplicatesFunc
func EchoFunc ¶
EchoFunc repeats each element for the returned amount of times using fn on each item.
func Filter ¶
Filter returns a modifier that constantly progresses the iterator to the next item matching pred
func FilterMap ¶
FilterMap returns a modifier that constantly progresses the iterator to the next item matching fn, and transforms it into an iterator for a different type.
func FlatMap ¶
FlatMap returns a modifier to map items to iterators and flattens them into a single iterator
func Inject ¶
Inject returns a modifier that injects items from the given iterator at the given index
func InjectFunc ¶
InjectFunc returns a modifier that injects items from the given iterator once fn returns true
func InsertFunc ¶
InsertFunc returns a modifier that inserts the given items once fn returns true
func Interpolate ¶
func Interpolate[T constraints.Float | constraints.Integer, S constraints.Float](start1, end1 T, start2, end2 S) Modifier[T, S]
Interpolate returns a modifier to map numbers into another range.
func JoinLeading ¶
JoinLeading returns a modifier to join an iterator to the end of the given leading iterator
func JoinTrailing ¶
JoinTrailing returns a modifier to join the given trailing iterator to the end of an iterator
func Limit ¶
Limit returns a modifier that stops the iterator when it has progressed times equal to limit
func Map ¶
Map returns a modifier that applies fn on each item from the iterator It will replace the item with the value returned from fn
func Mod ¶
func Mod[T constraints.Integer](x T) Modifier[T, T]
Mod returns a modifier to mod items with x.
func Mul ¶
func Mul[T constraints.Float | constraints.Integer](x T) Modifier[T, T]
Mul returns a modifier to multiply items by x.
func Normalize ¶
func Normalize[T constraints.Float | constraints.Integer, S constraints.Float](min, max T) Modifier[T, S]
Normalize returns a modifier that scales down numbers to be in the range [0, 1].
func Nwise ¶
Nwise returns a modifier that returns an iterator of iterators. Those iterators contain size items and starting from the first item, sliding up to the last item.
func Pow ¶
func Pow[T constraints.Float | constraints.Integer](x T) Modifier[T, T]
Pow returns a modifier to raise items to the xth power.
func Remove ¶
func Remove[T comparable](rem T) Modifier[T, T]
Remove returns a modifier that filters away items equal to rem
func RemoveFunc ¶
RemoveFunc returns a modifier that filters away items matching fn
func Repeat ¶
Repeat the items in the range [from, to) for as many times given. All other items outside this range are included in the iterator as well.
func Replace ¶
func Replace[T comparable](old T, new T, times int) Modifier[T, T]
Replace returns a modifier that changes occurances of old with new for as many times
func ReplaceAll ¶
func ReplaceAll[T comparable](old T, new T) Modifier[T, T]
ReplaceAll returns a modifier that changes all occurances of old with new
func RunsFunc ¶
RunsFunc is a modifier that splits the iterator into multiple iterators each containing matching consecutive items using fn to get a comparable key for each item.
func SamplesFunc ¶
SamplesFunc returns a modifier that returns random samples from the given iterator. It uses fn as a random number generator in the interval [0.0,1.0).
func Slice ¶
Slice returns a modifier which makes the iterator return items in the range [from, to) increasing by step.
func Splice ¶
Splice returns a modifier which replaces items in the range [from, to) with all items from injected.
func Split ¶
func Split[T comparable](sep T) Modifier[T, Iterator[T]]
Split returns a modifier that splits the iterator when it encounters an item equal to sep
func SplitFunc ¶
SplitFunc returns a modifier that splits the iterator when fn returns true The return values of fn also determine whether the item should be part of the firat part, second part, both or neither
func SplitLeading ¶
func SplitLeading[T comparable](sep T) Modifier[T, Iterator[T]]
SplitLeading is like Split but includes sep at the end of leading part.
func SplitTrailing ¶
func SplitTrailing[T comparable](sep T) Modifier[T, Iterator[T]]
SplitTrailing is like Split but includes sep at the start of trailing part.
func Sub ¶
func Sub[T constraints.Float | constraints.Integer](x T) Modifier[T, T]
Sub returns a modifier to subtract x from items.
func TakeWhile ¶
TakeWhile returns a modifier which makes the iterator return items matching pred and stops at the first item that does not.
func UniquesFunc ¶
func UniquesFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, T]
UniquesFunc