iterator

package module
v0.0.0-...-33668f2 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 8 Imported by: 0

README

iterator

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoItems    = errors.New("iterator: no items in iterator")
	ErrMultiItems = errors.New("iterator: multiple items in iterator")
)

Functions

func Async

func Async[T any, V any](iterator Iterator[T], fn func(Iterator[T]) (V, error)) <-chan ValErr[V]

Async calls fn with the given iterator in a separate goroutine and returns the result in a ValErr channel.

func Concat

func Concat[T ~string](iter Iterator[T], sep T) (T, error)

Concat all items with the given separator

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

func ContainsFunc[T any](iter Iterator[T], fn func(int, T) (bool, error)) (bool, error)

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 Fold

func Fold[T any, S any](iter Iterator[T], start S, fn func(int, T, S) (S, error)) (S, error)

Fold all items into a single value with a start value by applying fn on all items

func Iterate

func Iterate[T any](iterator Iterator[T], f func(int, T) (bool, error)) (int, error)

func Len

func Len[T any](iter Iterator[T]) (int, error)

Len exhausts the iterator to return its length

func One

func One[T any](iterator Iterator[T]) (T, error)

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 Reduce

func Reduce[T any](iter Iterator[T], fn func(int, T, T) (T, error)) (T, error)

Reduce all items into a single value by applying fn on all items

func String

func String[T any](iter Iterator[T]) (string, error)

String converts and concatenates all items into a single string

func Sum

Sum all numbers in the iterator

func ToChannel

func ToChannel[T any](iter Iterator[T], size int) (<-chan ValErr[T], func())

ToChannel consumes all items in the iterator into a channel with size as capacity

func ToFunc

func ToFunc[T any](iter Iterator[T]) func() (T, bool, error)

ToFunc returns a function to consume all items in the iterator

func ToMap

func ToMap[K comparable, V any](iter Iterator[KV[K, V]]) (map[K]V, error)

ToMap consumes all items in a KV iterator into a map

func ToSlice

func ToSlice[T any](iter Iterator[T]) ([]T, error)

ToSlice consumes all items in the iterator into a slice

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 Assoc

func Assoc[K comparable, V any](keys Iterator[K], values Iterator[V]) Iterator[KV[K, V]]

Assoc the given keys to the given values in a new map iterator

func Const

func Const[T any](value T) Iterator[T]

Const returns an infinite iterator with with the given value

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 Duplicates

func Duplicates[T comparable](iter Iterator[T]) Iterator[T]

Duplicates

func Easing

func Easing[T constraints.Float](n int, fn func(float64) float64) Iterator[T]

Easing returns an iterator using an easing function with as many values as n.

func Empty

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

Empty returns an empty iterator of type T

func Fibonacci

func Fibonacci[T constraints.Float | constraints.Integer]() Iterator[T]

Fibonacci returns an iterator for fibonacci numbers

func Flatten

func Flatten[T any](iter Iterator[Iterator[T]]) Iterator[T]

Flatten is a modifier that applies on iterator of iterators It flattens them into a single iterator

func FromChannel

func FromChannel[T any](source <-chan T) Iterator[T]

FromChannel returns an iterator wrapping a channel source

func FromFunc

func FromFunc[T any](next func() (T, bool, error)) Iterator[T]

FromFunc returns an iterator wrapping a func 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 FromSlice

func FromSlice[T any](source []T) Iterator[T]

FromSlice returns an iterator wrapping a slice source

func FromValErrChannel

func FromValErrChannel[T any](source <-chan ValErr[T]) Iterator[T]

FromValErrChannel returns an iterator wrapping a channel source with items of type ValErr

func Group

func Group[T comparable](iter Iterator[T]) Iterator[Iterator[T]]

Group

func Interleave

func Interleave[T any](count int, iters ...Iterator[T]) Iterator[T]

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 Join

func Join[T any](iters ...Iterator[T]) Iterator[T]

Join multiple iterators to form a larger iterator

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 Merge

func Merge[T any](iters ...Iterator[T]) Iterator[T]

Merge takes multiple iterators and consumes them concurrently into a single iterator

func Mirror

func Mirror[T any](iter Iterator[T], count int) []Iterator[T]

Mirror creates multiple synchronised iterators with the same underlying iterator

func OnClose

func OnClose[T any](iter Iterator[T], fn func() error) Iterator[T]

OnClose extends an iterator with a close callback

func Once

func Once[T any](value T) Iterator[T]

Once returns an iterator with with the given value and size 1

func Pipe

func Pipe[T any](iter Iterator[T], mods ...Modifier[T, T]) Iterator[T]

Pipe applies the modifiers to the given iterator and returns the resulting iterator

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.

func Strings

func Strings[T any](iter Iterator[T]) Iterator[string]

Strings is a modifier that converts all items to strings

func ToSlices

func ToSlices[T any](iter Iterator[Iterator[T]]) Iterator[[]T]

func Unfold

func Unfold[T any, S any](state S, fn func(int, S) (T, S, bool, error)) Iterator[T]

Unfold generates an iterator from fn using the given initial state

func Uniques

func Uniques[T comparable](iter Iterator[T]) Iterator[T]

Uniques

func Values

func Values[K comparable, V any](iter Iterator[KV[K, V]]) Iterator[V]

Values is a modifier that takes a map iterator and returns an iterator for the values

func Zero

func Zero[T any]() Iterator[T]

Zero returns an infinite iterator with the zero value of type T

type KV

type KV[K comparable, V any] struct {
	Key K
	Val V
}

type Modifier

type Modifier[T any, S any] func(Iterator[T]) Iterator[S]

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 Append

func Append[T any](items ...T) Modifier[T, T]

Append returns a modifier to append items to an iterator

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

func Boomerang[T any](times int) Modifier[T, T]

Boomerang repeats the iterator for as many times given and alternates between reverse and original order.

func Chunk

func Chunk[T any](size int) Modifier[T, Iterator[T]]

Chunk returns a modifier that splits the iterator into smaller chunks

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 Cycle

func Cycle[T any](times int) Modifier[T, T]

Cycle repeats the iterator for as many times given.

func DistinctFunc

func DistinctFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, T]

func Distribute

func Distribute[T any](buffer int) Modifier[T, Iterator[T]]

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

func DropWhile[T any](pred func(int, T) (bool, error)) Modifier[T, T]

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 Echo

func Echo[T any](times int) Modifier[T, T]

Echo repeats each element for the given times.

func EchoFunc

func EchoFunc[T any](fn func(int, T) (int, error)) Modifier[T, T]

EchoFunc repeats each element for the returned amount of times using fn on each item.

func Filter

func Filter[T any](pred func(int, T) (bool, error)) Modifier[T, T]

Filter returns a modifier that constantly progresses the iterator to the next item matching pred

func FilterMap

func FilterMap[T any, S any](fn func(int, T) (S, bool, error)) Modifier[T, S]

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

func FlatMap[T any, S any](fn func(int, int, T) (Iterator[S], error)) Modifier[T, S]

FlatMap returns a modifier to map items to iterators and flattens them into a single iterator

func GroupFunc

func GroupFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, Iterator[T]]

GroupFunc

func Inject

func Inject[T any](index int, in Iterator[T]) Modifier[T, T]

Inject returns a modifier that injects items from the given iterator at the given index

func InjectFunc

func InjectFunc[T any](in Iterator[T], fn func(int, T) (bool, error)) Modifier[T, T]

InjectFunc returns a modifier that injects items from the given iterator once fn returns true

func Insert

func Insert[T any](index int, items ...T) Modifier[T, T]

Insert returns a modifier that inserts the given items once fn returns true

func InsertFunc

func InsertFunc[T any](fn func(int, T) (bool, error), items ...T) Modifier[T, T]

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

func JoinLeading[T any](leading Iterator[T]) Modifier[T, T]

JoinLeading returns a modifier to join an iterator to the end of the given leading iterator

func JoinTrailing

func JoinTrailing[T any](trailing Iterator[T]) Modifier[T, T]

JoinTrailing returns a modifier to join the given trailing iterator to the end of an iterator

func Limit

func Limit[T any](limit int) Modifier[T, T]

Limit returns a modifier that stops the iterator when it has progressed times equal to limit

func Map

func Map[T any, S any](fn func(int, T) (S, error)) Modifier[T, S]

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

func Nwise[T any](size int) Modifier[T, Iterator[T]]

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 Offset

func Offset[T any](offset int) Modifier[T, T]

Offset returns a modifier that progresses the iterator times equal to offset

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 Prepend

func Prepend[T any](items ...T) Modifier[T, T]

Prepend returns a modifier to prepend items to an iterator

func Remove

func Remove[T comparable](rem T) Modifier[T, T]

Remove returns a modifier that filters away items equal to rem

func RemoveFunc

func RemoveFunc[T any](fn func(int, T) (bool, error)) Modifier[T, T]

RemoveFunc returns a modifier that filters away items matching fn

func Repeat

func Repeat[T any](from int, to int, times int) Modifier[T, T]

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

func RunsFunc[T any, S comparable](fn func(int, T) (S, error)) Modifier[T, Iterator[T]]

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 Samples

func Samples[T any](population, sample int) Modifier[T, T]

Samples returns a modifier that returns random samples from the given iterator.

func SamplesFunc

func SamplesFunc[T any](population, sample int, fn func() float64) Modifier[T, T]

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

func Slice[T any](from, to, step int) Modifier[T, T]

Slice returns a modifier which makes the iterator return items in the range [from, to) increasing by step.

func Splice

func Splice[T any](from, to int, injected Iterator[T]) Modifier[T, T]

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

func SplitFunc[T any](fn func(int, T) (split, inA, inB bool, err error)) Modifier[T, Iterator[T]]

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

func TakeWhile[T any](pred func(int, T) (bool, error)) Modifier[T, T]

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

type ValErr

type ValErr[T any] struct {
	Val T
	Err error
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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