Documentation ¶
Overview ¶
This package defines an iterator framework comprized of lazily evaluated, pull style iterators.
Index ¶
- func FilterToIndex[T any](num int) func(index int, val T) bool
- func NoOp[T any, U any](val T, res U, err error)
- func Parallel[T any, U any](i Iter[T], workerOp func(val T) (U, error), ...) error
- type Iter
- func ChanElems[T any](c <-chan T) Iter[T]
- func FileLines(path string) Iter[string]
- func Join[T any, U any](i1 Iter[T], i2 Iter[U], factory func() basic.Variant[T, U], ...) Iter[basic.Variant[T, U]]
- func JoinSame[T any](i1 Iter[T], i2 Iter[T], factory func() basic.Variant[T, T], ...) Iter[T]
- func Map[T any, U any](i Iter[T], op func(index int, val T) (U, error)) Iter[U]
- func MapElems[K comparable, V any](m map[K]V, factory func() basic.Pair[K, V]) Iter[basic.Pair[K, V]]
- func MapKeys[K comparable, V any](m map[K]V) Iter[K]
- func MapVals[K comparable, V any](m map[K]V) Iter[V]
- func Next[T any, U any](i Iter[T], ...) Iter[U]
- func NoElem[T any]() Iter[T]
- func PntrToVal[T any](i Iter[*T]) Iter[T]
- func Range[T ~int | ~int8 | ~int16 | ~int32 | ~int64](start T, stop T, jump T) Iter[T]
- func Recurse[T any](root Iter[T], shouldRecurse func(v T) bool, iterValToIter func(v T) Iter[T]) Iter[T]
- func SequentialElems[T any](_len int, get func(i int) (T, error)) Iter[T]
- func SliceElemPntrs[T any](s []T) Iter[*T]
- func SliceElems[T any](s []T) Iter[T]
- func StrElems(s string) Iter[byte]
- func ValElem[T any](val T, err error, repeat int) Iter[T]
- func ValToPntr[T any](i Iter[T]) Iter[*T]
- func Zip[T any, U any](i1 Iter[T], i2 Iter[U], factory func() basic.Pair[T, U]) Iter[basic.Pair[T, U]]
- func (i Iter[T]) All(op func(val T) (bool, error)) (bool, error)
- func (i Iter[T]) Any(op func(val T) (bool, error)) (bool, error)
- func (i Iter[T]) AppendTo(orig *[]T) (int, error)
- func (i Iter[T]) Collect() ([]T, error)
- func (i Iter[T]) Consume() error
- func (i Iter[T]) Count() (int, error)
- func (i Iter[T]) Filter(op func(index int, val T) bool) Iter[T]
- func (i Iter[T]) FilterParallel(op func(val T) bool, numThreads int) ([]T, error)
- func (i Iter[T]) Find(op func(val T) (bool, error)) (T, error, bool)
- func (i Iter[T]) ForEach(op func(index int, val T) (IteratorFeedback, error)) error
- func (i Iter[T]) Index(op func(val T) (bool, error)) (int, error)
- func (i Iter[T]) Inject(op func(idx int, val T, injectedPrev bool) (T, error, bool)) Iter[T]
- func (i Iter[T]) Map(op func(index int, val T) (T, error)) Iter[T]
- func (i Iter[T]) Next(...) Iter[T]
- func (i Iter[T]) Nth(idx int) (T, error, bool)
- func (i Iter[T]) Parallel(workerOp func(val T) (T, error), resOp func(val T, res T, err error), ...) error
- func (i Iter[T]) Pull(num int) ([]T, error, bool)
- func (i Iter[T]) PullOne() (T, error, bool)
- func (i Iter[T]) Reduce(start T, op func(accum *T, iter T) error) (T, error)
- func (i Iter[T]) Setup(setup func() error) Iter[T]
- func (i Iter[T]) SetupTeardown(setup func() error, teardown func() error) Iter[T]
- func (i Iter[T]) Skip(num int) Iter[T]
- func (i Iter[T]) Stop() error
- func (i Iter[T]) Take(num int) Iter[T]
- func (i Iter[T]) TakeWhile(op func(val T) bool) Iter[T]
- func (i Iter[T]) Teardown(teardown func() error) Iter[T]
- func (i Iter[T]) ToChan(c chan T)
- func (i Iter[T]) ToWriter(src io.Writer, addNewLine bool) error
- type IteratorFeedback
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FilterToIndex ¶
A helper function that can be passed to the [Filter] function that filters elements before a specified index value. Internally, this is how [Skip] is implemented.
func NoOp ¶
A helper function that can be passed to Parallel as a result operation that performs no action. Helpfull when you want to start a bunch of parallel jobs and do nothing with there results.
func Parallel ¶
func Parallel[T any, U any]( i Iter[T], workerOp func(val T) (U, error), resOp func(val T, res U, err error), numThreads int, ) error
This function is a consumer.
ForEachParallel will consumer it's parent iterators values and start a new go routine to process the result using the worker operation. As each worker finishes the result operation will be called on the workers results. The result operation is syncronized, so this could be where you collect the results from the parallel workers. The number of go routines that are spawned will be limited to numThreads, which must be >=1.
Types ¶
type Iter ¶
type Iter[T any] func(f IteratorFeedback) (T, error, bool)
Iter is the base type that the entire package is built from. This type defines the function that has methods defined on it such that they can be chained together to form iterator chains. The returned values are as follows:
- T: the value that will be produced in the iterator sequence
- error: any error that was generated when attempting to get the next value
in the iterator sequence
- bool: a flag to indicate whether or not to continue iteration
func ChanElems ¶
This function is a producer.
ChanElems returns an iterator that iterates over the elements in an unbuffered channel. Calling this function will block until the channel receives a value. This producer will never return an error. This funciton does not close the channel once it is done iterating. The channel may still have values present in it once iteration is complete depending on if any other functions in the iterator chain stop iteration early.
func FileLines ¶
This function is a producer.
FileLines returns an iterator that iterates over the lines in a file. If an error occurs opening the file then no lines will be iterated over and the error will be returned upon the first iteration of the producer.
func Join ¶
func Join[T any, U any]( i1 Iter[T], i2 Iter[U], factory func() basic.Variant[T, U], decider func(left T, right U) bool, ) Iter[basic.Variant[T, U]]
This function is a producer.
Join takes two iterators and a decider function and returns an iterator that consumes both supplied iterators, returning a single value at a time based on the return value from the decider function. The number of values returned will equal the total number of values returned from both of the supplied iterators. Errors from the supplied iterators will be returned by this iterator.
func JoinSame ¶
func JoinSame[T any]( i1 Iter[T], i2 Iter[T], factory func() basic.Variant[T, T], decider func(left T, right T) bool, ) Iter[T]
This function is a producer.
JoinSame takes two iterators and a decider function and returns an iterator that consumes both supplied iterators, returning a single value at a time based on the return value from the decider function. The number of values returned will equal the total number of values returned from both of the supplied iterators. Errors from the supplied iterators will be returned by this iterator.
func Map ¶
This function is an intermediary.
Map will create a mapping between value of one iterator and values of another iterator. Iteration will stop if an error is generated.
func MapElems ¶
func MapElems[K comparable, V any]( m map[K]V, factory func() basic.Pair[K, V], ) Iter[basic.Pair[K, V]]
This function is a producer.
MapElems returns an iterator that iterates over a maps key,value pairs. Do not confuse this with the Map intermediary function. This producer will never return an error. This producer is not thread safe. If the underlying map value it changed while being iterated over behavior is undefined.
func MapKeys ¶
func MapKeys[K comparable, V any](m map[K]V) Iter[K]
This function is a producer.
MapElems returns an iterator that iterates over a maps key values. Do not confuse this with the Map intermediary function. This producer will never return an error. This producer is not thread safe. If the underlying map value it changed while being iterated over behavior is undefined.
func MapVals ¶
func MapVals[K comparable, V any](m map[K]V) Iter[V]
This function is a producer.
MapElems returns an iterator that iterates over a maps values. Do not confuse this with the Map intermediary function. This producer will never return an error. This producer is not thread safe. If the underlying map value it changed while being iterated over behavior is undefined.
func Next ¶
func Next[T any, U any](i Iter[T], op func(index int, val T, status IteratorFeedback) (IteratorFeedback, U, error), ) Iter[U]
This function is an intermediary.
Next will take it's parent iterator and consume its values. As it does this it will apply the operation (op) function to the value before passing on the transformed value to it's child iterator. If an error is generated iteration will stop.
func NoElem ¶
This function is a producer.
NoElem provides an iterator that returns no elements. NoElem returns an empty iterator.
func PntrToVal ¶
This function is an intermediary.
PntrToVal will map a stream of pointers to values to a stream of values. PntrToVal will stop iteration if an error is returned from it's parent iterator regardless of if it has fully consumed it's input stream of values. PntrToVal will never be the cause of an error.
func Range ¶
This funciton is a producer.
Range returns an iterator that produces a sequence of values according to the values that are given as parameters. The sequence of values that will be returned starts with the start value, and increments by the amount specified by the jump parameter. It will stop once it reaches the stop value, exclusively, meaning the last value will not be included. There are no conditions to check for infinite loops. A jump of 0 will always result in a infinite loop as long as start!=stop. No errors will ever be returned by this producer.
func Recurse ¶
func Recurse[T any]( root Iter[T], shouldRecurse func(v T) bool, iterValToIter func(v T) Iter[T], ) Iter[T]
This function is a producer.
Recurse will return an iterator that recursively returns values from the supplied iterator. This iterator will enforce root-left-right traversal. This order is the only available order because once an iterator has produced a value there is no way to "push" it back.
Recurse takes a root iterator where the recursion begins. This iterator can return as many values as it needs, there is no limitation holding to only produce one value.
The shouldRecurse function should return true if the current value needs to be recursed upon.
The iterValToIter takes a value from an iterator and returns an iterator over that value. This is where the recursion happens.
func SequentialElems ¶
This function is a producer.
SequentialElems returns an iterator that iterates over a general container using the get function in combination with the length argument. Note that unlike the SliceElems and StrElems producers this producer can return an error.
func SliceElemPntrs ¶
This function is a producer.
SliceElemPntrs returns an iterator that iterates over the supplied slices elements, providing points to the elements in the slice rather than the elements themselves. No error will ever be returned by this producer. This producer is not thread safe. If the underlying slice is modified while it is being iterated over the behavior will be undefined. For a thread safe implementation of SliceElemPntrs use the SyncedVector.Elems method from the collections package.
func SliceElems ¶
This function is a producer.
SliceElems returns an iterator that iterates over the supplied slices elements. No error will ever be returned by this producer. This producer is not thread safe. If the underlying slice is modified while it is being iterated over the behavior will be undefined. For a thread safe implementation of SliceElems use the SyncedVector.Elems method from the collections package.
func StrElems ¶
This function is a producer.
StrElems returns an iterator that iterates over the supplied strings characters. No error will ever be returned by this producer.
func ValElem ¶
This function is a producer.
ValElem returns an iterator that produces the supplied value and error the supplied number of times. The same value and error will be returned so any modifications made to the value and error will be visible on subsiquent iterations. Any error other than nil will cause iteration to stop after the first value due to how the intermediaries and consumers are implemented.
func ValToPntr ¶
This function is an intermediary.
ValToPntr will map a stream of values to a stream of pointers to the values. ValToPntr will stop iteration if an error is returned from it's parent iterator regardless of if it has fully consumed it's input stream of values. ValToPntr will never be the cause of an error.
func Zip ¶
func Zip[T any, U any]( i1 Iter[T], i2 Iter[U], factory func() basic.Pair[T, U], ) Iter[basic.Pair[T, U]]
This function is a producer.
Zip will take two iterators and return an iterator that iterates over pairs of values where each pair contains a value from each supplied iterator. The number of values returned by this iterator will equal the number of elements from the supplied iterator that produces the least number values. Excess values will be ignored. Errors from the supplied iterators will be returned from this iterator.
func (Iter[T]) All ¶
This function is a Consumer.
All will apply the supplied operation function (op) to each value from it's parent iterator and will only return true if the operation function returns true for every value, otherwise it will return false. If an error occurs iteration will stop. If an error occurs, the state of the boolean value that All returns will reflect the result of applying the operation function to every value from the parent iterator before the error occurred, excluding the value returned with the error.
func (Iter[T]) Any ¶
This function is a Consumer.
Any will apply the supplied operation function (op) to each value from it's parent iterator and will only return true if the operation function returns true for any value, otherwise it will return false. If an error occurs iteration will stop. If an error occurs, the state of the boolean value that Any returns will reflect the result of applying the operation function to every value from the parent iterator before the error occurred, excluding the value returned with the error.
func (Iter[T]) AppendTo ¶
This function is a Consumer.
AppendTo will append all of it's parent iterators values into a slice. Iteration will stop if an error occurs. If an error occurs then the slice will contain all the values that were already present in it plus the values that were received up until the error occurred.
func (Iter[T]) Collect ¶
This function is a Consumer.
Collect will collect all of it's parent iterators values into a slice and return it. Iteration will stop if an error occurs. If an error occurs then the slice will contain all the values that were recieved up until that point.
func (Iter[T]) Consume ¶
This function is a Consumer.
Consume will consume its parent iterators entirely, only stopping early if it hits an error. The values from the parent iterator are not saved or used in any way.
func (Iter[T]) Count ¶
This function is a Consumer.
Count will consumer it's parent iterator, counting the number of values that it receives. The values from the parent iterator are not saved or used in any way. Iteration will stop if an error occurs.
func (Iter[T]) Filter ¶
This function is an intermediary.
Filter will selectively pass on values from it's parent iterator to its child iterator based on the return value of the operatio (op) function. An error will stop iteration and be propogated to the child iterator regardless of the implementation of the operation function.
func (Iter[T]) FilterParallel ¶
This funciton is a consumer.
This function will filter each value from it's parent iterator in parallel. The provided operation function will be run in parallel using numThreads go routines. The operation function will return true should return true if the value should be kept and false if it should not be kept. Use FilterParallel when the operation that determines which values to filter takes a non-trivial amount of time.
func (Iter[T]) Find ¶
This function is a Consumer.
Find will search for a value in it's parent iterators values using the supplied operation function (op) as an equality comparison. Find returns three values:
1. The value that was found 2. An error status that represents errors raised during iteration or from the equality comparison function. 3. A boolean value that indicates if the value was found or not.
If an error occurs iteration stops.
func (Iter[T]) ForEach ¶
func (i Iter[T]) ForEach( op func(index int, val T) (IteratorFeedback, error), ) error
This funciton is a consumer.
For each will take values from it's parent iterator and perform the supplied operation function on each value, until an error occurs. If an error occurs the operation function is not called and iteration stops.
func (Iter[T]) Index ¶
This function is a Consumer.
Index will search for a value in it's parent iterators values using the supplied operation function (op) as an equality comparison. The index of the value will be returned, or -1 if the value is not found. If an error occurs iteration will stop.
func (Iter[T]) Inject ¶
This function is an intermediary.
Inject will inject values into the iterator stream based on the operation (op) functions return values. If the operation function returns true then the value will be injected and the parent iterators current value will be cached to be returned as soon as the operation function returns false. This provides a way to arbitrarily change the sequence of values that is output. If an error is returned from either the parent iterator or the operation function then iteration will stop.
func (Iter[T]) Map ¶
This function is an intermediary.
Map will create a mapping between two iterators of the same type. This is equivalent the calling the previous Map function and providing it with the same types. Iteration will stop if an error is generated.
func (Iter[T]) Next ¶
func (i Iter[T]) Next( op func(index int, val T, status IteratorFeedback) (IteratorFeedback, T, error), ) Iter[T]
This function is an intermediary.
This function is equivalent to Next, the only difference is that the the inputs iterator type and output iterator type must be the same. It is offered as a convenience function.
func (Iter[T]) Nth ¶
This function is a Consumer.
Nth will return the value at the nth index from it's parent consumer. Nth returns three values:
1. The value that was found 2. An error status that represents errors raised during iteration 3. A boolean value that indicates if the nth index was reached or not
If an error occurs iteration will stop.
func (Iter[T]) Parallel ¶
func (i Iter[T]) Parallel( workerOp func(val T) (T, error), resOp func(val T, res T, err error), numThreads int, ) error
This function is a consumer.
This function is equivalent to Parallel, the only difference is that the the inputs and outputs of the workers must be the same. It is offered as a convenience function.
func (Iter[T]) Pull ¶
This function is a consumer.
Pull will pull num values from the iterator chain, until the end of the iterator chain has been reached. It will return the values, an error if one occurred while obtaining the values, and a boolean flag to indicate success. The values that is returned should be assumed to be valid, even if err is not nil or the boolean flag is false. This function *does not* clean up the iterator chain once the chains producer has reached the end of its stream of values. Stop must be called manually to do this. Continuing to call Pull after the end of the iterator stream has been reached will not result in any undefined behavior.
func (Iter[T]) PullOne ¶
This function is a consumer.
PullOne will pull one value from the iterator chain. It will return the value, an error if one occurred while obtaining the value, and a boolean flag to indicate success. The value that is returned should be assumed to be invalid if err is not nil or the boolean flag is false. This function *does not* clean up the iterator chain once the chains producer has reached the end of its stream of values. Stop must be called manually to do this. Continuing to call PullOne after the end of the iterator stream has been reached will not result in any undefined behavior.
func (Iter[T]) Reduce ¶
This function is a Consumer.
Reduce will take all of the values from it's parent iterator and will combine them using the logic from the operation function (op), returning the combined value. The start value is specified as an argument. If an error occurs then iteration will stop and the current accumulated value will be returned without applying the operation function to the value that returned an error.
func (Iter[T]) Setup ¶
This function is an intermediary.
Setup will call the provided setup function before it calls its parent iterator. The setup function will only be called once. An error returned from the setup function will stop iteration and the parent iterator will never be called.
func (Iter[T]) SetupTeardown ¶
This function is an intermediary.
SetupTeardown provides a way to have setup and teardown procedures. These setup and teardown procedures will be called once before the parent iterator is ever called and after once after the parent iterator has completed. The teardown procedure will always be called even if the parent iterator returned an error.
func (Iter[T]) Skip ¶
This function is an intermediary.
Skip will skip the first num elements of it's parent iterator before propagating any further elements to it's child iterator. Skip will stop iteration if an error is returned from it's parent iterator regardless of if it has reached num elements or not.
func (Iter[T]) Stop ¶
This function is a consumer.
Stop will stop all iteration without ever consuming a single value from it's parent iterator. Any errors returned will be generated from each iterator performing there respective teardown operations.
func (Iter[T]) Take ¶
This function is an intermediary.
Take will consume the first num elements of it's parent iterator. It will stop iteraton after the first num elements have been consumed. If an error occurs iteraton will stop regardless of if num elements have been consumed.
func (Iter[T]) TakeWhile ¶
This function is an intermediary.
Take while will take elements while the supplied operation (op) returns true. Once the supplied operation returns false iteration will stop. If an error is returned from the parent iterator iteration will stop and the operation function will not be called on the value that errored.
func (Iter[T]) Teardown ¶
This function is an intermediary.
Teardown will call the provided teardown function once it's parent iterator has completed iteration. Teardown will only be called it iterator has started. If iteration never began due to an early error teardown will not be called. The teardown function will only be called once. An error returned from the teardown function will stop iteration.
func (Iter[T]) ToChan ¶
func (i Iter[T]) ToChan(c chan T)
This function is a Consumer.
ToChan will take all the values from it's parent iterator and will push them to the specified channel. Errors are not propogated to the channel. Iteration will stop if an error occurs. This function will not close the channel that is supplied to it.
func (Iter[T]) ToWriter ¶
This function is a Consumer.
ToWriter will take all the values from it's parent iterator and will write them to the specified writer. All values are written to the writer using the standard fmt.Sprintf %v formatting directive. If you want a format other than that then map the values to the formatted string before passing values to this iterator.
If addNewLine is true then newlines will be appended to every value after it is formatted.
type IteratorFeedback ¶
type IteratorFeedback int
A type that defines the valid states that an iterator chain can use.
const ( Continue IteratorFeedback = iota // Signaling to continue iteration, consume the next value Break // Signaling to stop iteration. The current value is no longer valid. Iterate // Signaling to iterate again, get the next value from the iterator. )