Documentation
¶
Overview ¶
Package iterator defines an interface to represent lazy iteration of values. Also includes types and functions for interacting with iterators.
Index ¶
- func All[E any](iter Iterator[E], predicate func(E) bool) (bool, error)
- func Any[E any](iter Iterator[E], predicate func(E) bool) (bool, error)
- func Fold[ELEMENT any, RESULT any](iter Iterator[ELEMENT], fold func(ELEMENT, RESULT) (RESULT, error), ...) (RESULT, error)
- func Reduce[ELEMENT any](iter Iterator[ELEMENT], reduce func(ELEMENT, ELEMENT) (ELEMENT, error)) (ELEMENT, error)
- func SendToChannel[E any](iter Iterator[E], c chan Result[E])
- func ToSlice[E any](iter Iterator[E]) (result []E, err error)
- type Channel
- type Clonable
- type CloneNoOp
- type Combine
- type Echo
- type Empty
- type Error
- type Filter
- type FilterMap
- type Generate
- type Generator
- type Iterator
- type Limit
- type Map
- type Result
- type Slice
- type StopWhen
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fold ¶
func Fold[ELEMENT any, RESULT any](iter Iterator[ELEMENT], fold func(ELEMENT, RESULT) (RESULT, error), init RESULT) (RESULT, error)
Fold uses the provided function to update the result with each element from the iterator. Returns the final result.
func Reduce ¶
func Reduce[ELEMENT any](iter Iterator[ELEMENT], reduce func(ELEMENT, ELEMENT) (ELEMENT, error)) (ELEMENT, error)
Reduce applies the provided functions to each element in the iterator along with an acculator of the same type.
func SendToChannel ¶
Types ¶
type Clonable ¶
type Clonable[E any] interface { Clone() E }
Clonable types are able to return a copy of themselves. Usually this copy has newly allocated space for all the mutable components.
type CloneNoOp ¶
type CloneNoOp[E any] struct { Wrap E }
CloneNoOp breaks the convention of the Clonable type by having Clone just return the same value. This is to allow convenient use of the Echo iterator without needing to implement `Clone`. Warning: Reference types will preserve transformations.
type Combine ¶
type Filter ¶
type Filter[E any] struct { Iterator[E] // Test should return true if the provided value should be included. Test func(E) bool }
Filter is an Iterator that wraps another iterator. It only returns the values that the Test function returns true for.
type FilterMap ¶
type Generate ¶
Generate is the function used by Generator to create the next value. The parameter is the previous value returned by the iterator. The first call to Generate will be passed the zero value of E. The returns should have the following valid structures - return some_value, nil. Continue iterating, some_value will be the next Get Value - return zero_of_E, some_error. An error occurred during Generate, Iteration Error. - return zero_of_E, Stop. Iteration is finished.
type Iterator ¶
type Iterator[E any] interface { // Next returns true when there is a value to be accessed with Get. // False either means that the Iterator is empty or an error occurred. Next() bool // Get returns the current value of the Iterator. Get should return the same // value until Next is called. Get's behavior is undefined if called before // Next or when Next returns false. Get() E // Err returns any errors occurred. if Next returns true, this should return nil Err() error }
Iterator is the primary interface this package defines. The basic usage of an Iterator looks like this.
```golang
for iter.Next() { doSomething(iter.Get()) } if err := iter.Err(); err != nil { panic(err) }
````
func MapAsync ¶
func MapAsync[BEFORE any, AFTER any](iter Iterator[BEFORE], update func(BEFORE) (AFTER, error), n int) Iterator[AFTER]
MapAsync applies the update function inside a separate go routines.
func NewChannelIterator ¶
func Range ¶
func Range[NUM constraints.Integer | constraints.Float](vals ...NUM) Iterator[NUM]
Range returns an iterator from a start number to an end number, not including the end value. Range interprets the number of parameters as follows: - Range() => empty Iterator, Next will return false. - Range(end) => an Iterator over 0 <= val < end, step = 1. - Range(start, end) => an Iterator over start <= val < end, step = 1. - Range(start, end, step) => an Iterator over start <= val < end, by the step value. any parameters past the first 3 are ignored.
type Map ¶
Map is an Iterator that wraps another iterator. It applies the Convert function to each value. This can result in a change in the iterating type.