Documentation ¶
Overview ¶
Package iters provides thread-safe iterators usable with Go’s for statement
Index ¶
- type BaseIterator
- type Converter
- type ConverterFunction
- type Empty
- func (i *Empty[T]) Cancel(errp ...*error) (err error)
- func (i *Empty[T]) Cond(iterationVariablep *T, errp ...*error) (condition bool)
- func (i *Empty[T]) Init() (iterationVariable T, iterator Iterator[T])
- func (i *Empty[T]) Next() (value T, hasValue bool)
- func (i *Empty[T]) Same() (value T, hasValue bool)
- type Function
- type IntegerIterator
- func (i *IntegerIterator[T]) Cancel(errp ...*error) (err error)
- func (i *IntegerIterator[T]) Cond(iterationVariablep *T, errp ...*error) (condition bool)
- func (i *IntegerIterator[T]) Init() (iterationVariable T, iterator Iterator[T])
- func (i *IntegerIterator[T]) Next() (value T, hasValue bool)
- func (i *IntegerIterator[T]) Same() (value T, hasValue bool)
- type Iterator
- func NewConverterIterator[K any, V any](keyIterator Iterator[K], converter ConverterFunction[K, V], ...) (iterator Iterator[V])
- func NewEmptyIterator[T any]() (iterator Iterator[T])
- func NewFunctionIterator[T any](iteratorFunction IteratorFunction[T], asyncCancel ...func()) (iterator Iterator[T])
- func NewIntegerIterator[T constraints.Integer](firstValue, lastValue T) (iterator Iterator[T])
- func NewSimpleConverterIterator[K any, V any](keyIterator Iterator[K], simpleConverter SimpleConverter[K, V], ...) (iterator Iterator[V])
- func NewSimpleFunctionIterator[T any](iteratorFunction SimpleIteratorFunc[T], asyncCancel ...func()) (iterator Iterator[T])
- func NewSlice1Iterator[T any](slice []T) (iterator Iterator[T])
- func NewSliceInterfaceIterator[I any, E any](slice []E) (iterator Iterator[I])
- func NewSliceIterator[T any](slice []T) (iterator Iterator[T])
- func NewSlicePointerIterator[E any](slice []E) (iterator Iterator[*E])
- func NewSlicePointerIteratorField[E any](fieldp *SlicePointer[E], slice []E) (iterator Iterator[*E])
- type IteratorAction
- type IteratorFunction
- type NextAction
- type Simple
- type SimpleConverter
- type SimpleFunc
- type SimpleIteratorFunc
- type Slice
- type Slice1
- type SliceInterface
- type SlicePointer
- func (i *SlicePointer[E]) Cancel(errp ...*error) (err error)
- func (i *SlicePointer[E]) Cond(iterationVariablep **E, errp ...*error) (condition bool)
- func (i *SlicePointer[E]) Init() (iterationVariable *E, iterator Iterator[*E])
- func (i *SlicePointer[T]) Next() (value *T, hasValue bool)
- func (i *SlicePointer[T]) Same() (value *T, hasValue bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseIterator ¶ added in v0.4.117
type BaseIterator[T any] struct { // contains filtered or unexported fields }
BaseIterator implements:
- Iterator methods [Iterator.Cond] [Iterator.Next] [Iterator.Cancel]
- consumer must:
- — implement [Iterator.Init] since it returns the enclosing type
- — provide IteratorAction[T]
func NewBaseIterator ¶ added in v0.4.117
func NewBaseIterator[T any]( iteratorAction IteratorAction[T], asyncCancel ...func(), ) (iterator *BaseIterator[T])
NewBaseIterator returns an implementation of Cond Next Cancel methods part of iters.Iterator
- asyncCancel is used if function or converter iterators are blocking. asyncCancel indicates that a Cancel invocation has occurred
func (*BaseIterator[T]) Cancel ¶ added in v0.4.117
func (i *BaseIterator[T]) Cancel(errp ...*error) (err error)
Cancel stops an iteration
- after Cancel invocation, Cond, Next and Same indicate no value available
- Cancel returns the first error that occurred during iteration, if any
- an iterator implementation may require Cancel invocation to release resources
- Cancel is deferrable
func (*BaseIterator[T]) Cond ¶ added in v0.4.118
func (i *BaseIterator[T]) Cond(iterationVariablep *T, errp ...*error) (condition bool)
Cond implements the condition statement of a Go “for” clause
- condition is true if iterationVariable was assigned a value and the iteration should continue
- the iterationVariable is updated by being provided as a pointer. iterationVariable cannot be nil
- errp is an optional error pointer receiving any errors during iterator execution
Usage:
for i, iterator := iters.NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
func (*BaseIterator[T]) Next ¶ added in v0.4.135
func (i *BaseIterator[T]) Next() (value T, hasValue bool)
Next advances to next item and returns it
- if hasValue true, value contains the next value
- otherwise, no more items exist and value is the data type zero-value
type Converter ¶ added in v0.4.135
type Converter[K any, V any] struct { *BaseIterator[V] // contains filtered or unexported fields }
Converter traverses another iterator and returns converted values
type ConverterFunction ¶ added in v0.4.117
ConverterFunction is the signature used by NewConverterIterator
- ConverterFunction receives a key and returns the corresponding value.
- if isCancel true, it means this is the last invocation of ConverterFunction and ConverterFunction should release any resources. Any returned value is not used
- ConverterFunction signals end of values by returning parl.ErrEndCallbacks. Any returned value is not used
- if ConverterFunction returns error, it will not be invoked again. Any returned value is not used
- ConverterFunction must be thread-safe
- ConverterFunction is invoked by at most one thread at a time
type Empty ¶ added in v0.4.135
type Empty[T any] struct{}
Empty is an iterator with no values. thread-safe.
type Function ¶ added in v0.4.135
type Function[T any] struct { // BaseIterator implements the DelegateAction[T] function required by // Delegator[T] and Cancel // - provides its delegateAction method to Delegator *BaseIterator[T] // contains filtered or unexported fields }
Function traverses a function generating values
type IntegerIterator ¶ added in v0.4.135
type IntegerIterator[T constraints.Integer] struct { // contains filtered or unexported fields }
func (*IntegerIterator[T]) Cancel ¶ added in v0.4.135
func (i *IntegerIterator[T]) Cancel(errp ...*error) (err error)
Cancel stops an iteration
- after Cancel invocation, Cond, Next and Same indicate no value available
- Cancel returns the first error that occurred during iteration, if any
- an iterator implementation may require Cancel invocation to release resources
- Cancel is deferrable
func (*IntegerIterator[T]) Cond ¶ added in v0.4.135
func (i *IntegerIterator[T]) Cond(iterationVariablep *T, errp ...*error) (condition bool)
Cond implements the condition statement of a Go “for” clause
- condition is true if iterationVariable was assigned a value and the iteration should continue
- the iterationVariable is updated by being provided as a pointer. iterationVariable cannot be nil
- errp is an optional error pointer receiving any errors during iterator execution
Usage:
for i, iterator := iters.NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
func (*IntegerIterator[T]) Init ¶ added in v0.4.135
func (i *IntegerIterator[T]) Init() (iterationVariable T, iterator Iterator[T])
Init implements the right-hand side of a short variable declaration in the init statement of a Go “for” clause
for i, iterator := iters.NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
func (*IntegerIterator[T]) Next ¶ added in v0.4.135
func (i *IntegerIterator[T]) Next() (value T, hasValue bool)
Next advances to next item and returns it
- if hasValue true, value contains the next value
- otherwise, no more items exist and value is the data type zero-value
func (*IntegerIterator[T]) Same ¶ added in v0.4.135
func (i *IntegerIterator[T]) Same() (value T, hasValue bool)
Same returns the same value again
- if hasValue true, value is valid
- otherwise, no more items exist and value is the data type zero-value
- If Next or Cond has not been invoked, Same first advances to the first item
type Iterator ¶
type Iterator[T any] interface { // Init implements the right-hand side of a short variable declaration in // the init statement of a Go “for” clause // // for i, iterator := iters.NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // // i is pointer to slice element Init() (iterationVariable T, iterator Iterator[T]) // Cond implements the condition statement of a Go “for” clause // - condition is true if iterationVariable was assigned a value and the iteration should continue // - the iterationVariable is updated by being provided as a pointer. // iterationVariable cannot be nil // - errp is an optional error pointer receiving any errors during iterator execution // // Usage: // // for i, iterator := iters.NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // // i is pointer to slice element Cond(iterationVariablep *T, errp ...*error) (condition bool) // Next advances to next item and returns it // - if hasValue true, value contains the next value // - otherwise, no more items exist and value is the data type zero-value Next() (value T, hasValue bool) // Cancel stops an iteration // - after Cancel invocation, Cond, Next and Same indicate no value available // - Cancel returns the first error that occurred during iteration, if any // - an iterator implementation may require Cancel invocation // to release resources // - Cancel is deferrable Cancel(errp ...*error) (err error) }
Iterator allows traversal of values.
- iterate over an unimported type using an iterator returning a derived value such as an interface type
- convert a slice value-by-value, ie. return interface-type values from a struct slice
- iterate over pointers to slice values
- iterate over function
- obtain errors occurring in the iterator or release iterator resources
The iterator interface is optimized for use in the Go “for” clause. Iterators in the iters package are thread-safe but thread-safety depends on iterator implementation.
Usage in Go “for” clause:
for i, iterator := NewIterator().Init(); iterator.Cond(&i); { println(i) var err error for i, iterator := NewIterator().Init(); iterator.Cond(&i, &err); { println(i) } if err != nil { var err error var iterator = NewIterator() for i := 0; iterator.Cond(&i); { println(i) } if err = iterator.Cancel(); err != nil {
func NewConverterIterator ¶
func NewConverterIterator[K any, V any]( keyIterator Iterator[K], converter ConverterFunction[K, V], asyncCancel ...func(), ) (iterator Iterator[V])
NewConverterIterator returns a converting iterator.
- converterFunction receives cancel and can return error
- ConverterIterator is thread-safe and re-entrant.
- stores self-referencing pointers
func NewEmptyIterator ¶
NewEmptyIterator returns an empty iterator of values type T.
- EmptyIterator is thread-safe.
func NewFunctionIterator ¶
func NewFunctionIterator[T any]( iteratorFunction IteratorFunction[T], asyncCancel ...func(), ) (iterator Iterator[T])
NewFunctionIterator returns an Iterator iterating over a function
- thread-safe
func NewIntegerIterator ¶ added in v0.4.135
func NewIntegerIterator[T constraints.Integer](firstValue, lastValue T) (iterator Iterator[T])
func NewSimpleConverterIterator ¶ added in v0.4.135
func NewSimpleConverterIterator[K any, V any]( keyIterator Iterator[K], simpleConverter SimpleConverter[K, V], asyncCancel ...func(), ) (iterator Iterator[V])
NewConverterIterator returns a converting iterator.
- converterFunction receives cancel and can return error
- ConverterIterator is thread-safe and re-entrant.
- stores self-referencing pointers
func NewSimpleFunctionIterator ¶ added in v0.4.135
func NewSimpleFunctionIterator[T any]( iteratorFunction SimpleIteratorFunc[T], asyncCancel ...func(), ) (iterator Iterator[T])
NewFunctionIterator returns an Iterator iterating over a function
- thread-safe
func NewSlice1Iterator ¶ added in v0.4.143
NewSliceIterator returns an iterator iterating over slice T values
- thread-safe
- uses non-pointer atomics
func NewSliceInterfaceIterator ¶ added in v0.4.125
NewSliceInterfaceIterator returns an iterator over slice []E returning those elements as interface I values
- [I ~*E, E any] as an expression of interface I implemented by &E does not actually hold true at compile time. The check whether I is an interface implemented by &E has to take place at runtime
- uses self-referencing pointers
func NewSliceIterator ¶
NewSliceIterator returns an iterator iterating over slice T values
- thread-safe
- uses non-pointer atomics
func NewSlicePointerIterator ¶ added in v0.4.117
NewSlicePointerIterator returns an iterator of pointers to T
- the difference is that:
- instead of copying a value from the slice,
- a pointer to the slice value is returned
- the returned Iterator value cannot be copied, the pointer value must be used
- uses self-referencing pointers
func NewSlicePointerIteratorField ¶ added in v0.4.125
func NewSlicePointerIteratorField[E any](fieldp *SlicePointer[E], slice []E) (iterator Iterator[*E])
type IteratorAction ¶ added in v0.4.135
IteratorAction is a delegated request from iters.BaseIterator
- isCancel true requests cancel of iteration. No further invocations will occur. The iterator should release resources. The iterator may return an error
- otherwise, the iterator can:
- — return the next value
- — return an error. No further invocations will occur
- — return err == parl.ErrEndCallbacks requesting an end to iterations. No further invocations will occur ErrEndCallbacks error is not returned to the consumer
- the returned value is used if:
- — returned err is nil and
- — provided isCancel was false and
- — returned didCancel is false
- isPanic indicates that err is the result of a panic. isPanic is only used if err is non-nil
type IteratorFunction ¶ added in v0.4.117
IteratorFunction is the signature used by NewFunctionIterator
- if isCancel true, it means this is the last invocation of IteratorFunction and IteratorFunction should release any resources. Any returned value is not used
- IteratorFunction signals end of values by returning parl.ErrEndCallbacks. Any returned value is not used
- if IteratorFunction returns error, it will not be invoked again. Any returned value is not used
- IteratorFunction must be thread-safe
- IteratorFunction is invoked by at most one thread at a time
type NextAction ¶
type NextAction bool
NextAction is a unique named type that indicates whether the next or the same value again is sought by Delegate.Next
- IsSame IsNext
const ( // IsSame indicates to Delegate.Next that // this is a Same-type incovation IsSame NextAction = false // IsNext indicates to Delegate.Next that // this is a Next-type incovation IsNext NextAction = true )
func (NextAction) String ¶
func (a NextAction) String() (s string)
type Simple ¶ added in v0.4.135
type Simple[K any, V any] struct { // BaseIterator implements Cancel and the DelegateAction[T] function required by // Delegator[T] // - receives invokeConverterFunction function // - provides delegateAction function BaseIterator[V] // contains filtered or unexported fields }
Converter traverses another iterator and returns converted values
type SimpleConverter ¶ added in v0.4.135
type SimpleFunc ¶ added in v0.4.135
type SimpleFunc[T any] struct { // BaseIterator implements the DelegateAction[T] function required by // Delegator[T] and Cancel // - provides its delegateAction method to Delegator *BaseIterator[T] // contains filtered or unexported fields }
Function traverses a function generating values
func (*SimpleFunc[T]) Init ¶ added in v0.4.135
func (i *SimpleFunc[T]) Init() (iterationVariable T, iterator Iterator[T])
Init implements the right-hand side of a short variable declaration in the init statement for a Go “for” clause
Usage:
for i, iterator := NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
type SimpleIteratorFunc ¶ added in v0.4.135
type Slice ¶ added in v0.4.135
type Slice[T any] struct { // contains filtered or unexported fields }
Slice traverses a slice container. thread-safe
func (*Slice[T]) Cancel ¶ added in v0.4.135
Cancel release resources for this iterator. Thread-safe
- not every iterator requires a Cancel invocation
func (*Slice[T]) Cond ¶ added in v0.4.135
Cond implements the condition statement of a Go “for” clause
- the iterationVariable is updated by being provided as a pointer. iterationVariable cannot be nil
- errp is an optional error pointer receiving any errors during iterator execution
- condition is true if iterationVariable was assigned a value and the iteration should continue
Usage:
for i, iterator := NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
type Slice1 ¶ added in v0.4.143
type Slice1[T any] struct { // contains filtered or unexported fields }
Slice traverses a slice container. thread-safe
func (*Slice1[T]) Cancel ¶ added in v0.4.143
Cancel release resources for this iterator. Thread-safe
- not every iterator requires a Cancel invocation
func (*Slice1[T]) Cond ¶ added in v0.4.143
Cond implements the condition statement of a Go “for” clause
- the iterationVariable is updated by being provided as a pointer. iterationVariable cannot be nil
- errp is an optional error pointer receiving any errors during iterator execution
- condition is true if iterationVariable was assigned a value and the iteration should continue
Usage:
for i, iterator := NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
type SliceInterface ¶ added in v0.4.135
type SliceInterface[I any, E any] struct { SlicePointer[E] }
func (*SliceInterface[I, E]) Cond ¶ added in v0.4.135
func (i *SliceInterface[I, E]) Cond(iterationVariablep *I, errp ...*error) (condition bool)
Cond updates I interface pointers
Usage:
for i, iterator := NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
func (*SliceInterface[I, E]) Init ¶ added in v0.4.135
func (i *SliceInterface[I, E]) Init() (iterationVariable I, iterator Iterator[I])
Init initializes I interface values and returns an I iterator
Usage:
for i, iterator := NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
func (*SliceInterface[I, E]) Next ¶ added in v0.4.135
func (i *SliceInterface[I, E]) Next() (value I, hasValue bool)
Next advances to next item and returns it
- if hasValue true, value contains the next value
- otherwise, no more items exist and value is the data type zero-value
func (*SliceInterface[I, E]) Same ¶ added in v0.4.135
func (i *SliceInterface[I, E]) Same() (value I, hasValue bool)
Same returns the same value again
- if hasValue true, value is valid
- otherwise, no more items exist and value is the data type zero-value
- If Next or Cond has not been invoked, Same first advances to the first item
type SlicePointer ¶ added in v0.4.135
type SlicePointer[E any] struct { // contains filtered or unexported fields }
SlicePointer traverses a slice container using pointers to value. thread-safe.
- the difference is that:
- instead of copying a value from the slice,
- a pointer to the slice value is returned
func (*SlicePointer[E]) Cancel ¶ added in v0.4.135
func (i *SlicePointer[E]) Cancel(errp ...*error) (err error)
Cancel release resources for this iterator. Thread-safe
- not every iterator requires a Cancel invocation
func (*SlicePointer[E]) Cond ¶ added in v0.4.135
func (i *SlicePointer[E]) Cond(iterationVariablep **E, errp ...*error) (condition bool)
Cond implements the condition statement of a Go “for” clause
- the iterationVariable is updated by being provided as a pointer. iterationVariable cannot be nil
- errp is an optional error pointer receiving any errors during iterator execution
- condition is true if iterationVariable was assigned a value and the iteration should continue
Usage:
for i, iterator := NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
func (*SlicePointer[E]) Init ¶ added in v0.4.135
func (i *SlicePointer[E]) Init() (iterationVariable *E, iterator Iterator[*E])
Init implements the right-hand side of a short variable declaration in the init statement for a Go “for” clause
Usage:
for i, iterator := NewSlicePointerIterator(someSlice).Init(); iterator.Cond(&i); { // i is pointer to slice element
func (*SlicePointer[T]) Next ¶ added in v0.4.135
func (i *SlicePointer[T]) Next() (value *T, hasValue bool)
Next advances to next item and returns it
- if hasValue true, value contains the next value
- otherwise, no more items exist and value is the data type zero-value
func (*SlicePointer[T]) Same ¶ added in v0.4.135
func (i *SlicePointer[T]) Same() (value *T, hasValue bool)
Same returns the same value again
- if hasValue true, value is valid
- otherwise, no more items exist and value is the data type zero-value
- If Next or Cond has not been invoked, Same first advances to the first item