Documentation ¶
Overview ¶
Package liter provides an iterator. To correctly implement an iterator, it should be initilized in a valid state so that this for loop would visit all the values:
for t,done := i.Cur(); !done; t,done = i.Next(){...}
Index ¶
- func Channel[T any](i Iter[T], buf int) <-chan T
- func Concurrent[T any](i Iter[T], fn func(t T, idx int)) *sync.WaitGroup
- func For[T any](i Iter[T], fn func(t T))
- func ForIdx[T any](i Iter[T], fn func(t T, idx int)) int
- func Pop[T any](i Iter[T]) T
- type Factory
- func (f Factory[T]) Channel(buf int) <-chan T
- func (f Factory[T]) Concurrent(fn func(t T, idx int)) *sync.WaitGroup
- func (f Factory[T]) For(fn func(t T))
- func (f Factory[T]) ForIdx(fn func(t T, idx int)) int
- func (f Factory[T]) Seek(fn func(t T) bool) Iter[T]
- func (f Factory[T]) Wrap() (iter Wrapper[T], t T, done bool)
- type Iter
- type Nested
- type Reducer
- type Starter
- type Wrapper
- func (w Wrapper[T]) Channel(buf int) <-chan T
- func (w Wrapper[T]) Concurrent(fn func(t T, idx int)) *sync.WaitGroup
- func (w Wrapper[T]) For(fn func(t T))
- func (w Wrapper[T]) ForIdx(fn func(t T, idx int)) int
- func (w Wrapper[T]) Pop() T
- func (w Wrapper[T]) Seek(fn func(t T) bool) Iter[T]
- func (w Wrapper[T]) Seq() func(yield func(T) bool)
- func (w Wrapper[T]) Seq2() func(yield func(int, T) bool)
- func (w Wrapper[T]) Wrapped() any
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Channel ¶
Channel creates a chan with size buf and places each value from Iter on the channel until Done is true. This does not reset the iterator. The channel is filled from a Go routine so all the values need to be consumed or the routine will never close.
func Concurrent ¶
Concurrent calls fn in a Go routine for each value Iter returns until Done is true. The returned WaitGroup will reach zero when all Go routines return. This does not reset the iterator.
func For ¶
For calls fn sequentially for each value Iter. This does not reset the iterator.
Example ¶
si := &sliceIter[int]{ Slice: []int{3, 1, 4, 1, 5, 9}, } fn := func(i int) { fmt.Println(i) } liter.For[int](si, fn)
Output: 3 1 4 1 5 9
Types ¶
type Factory ¶
Factory creates an iterator.
func (Factory[T]) Channel ¶
Channel creates a new Iter from the factory and creates a chan with size buf and places each value from Iter on the channel until Done is true. The channel is filled from a Go routine so all the values need to be consumed or the routine will never close.
func (Factory[T]) Concurrent ¶
Concurrent creates a new Iter from the factory and calls fn in a Go routine for each value Iter returns until Done is true. The returned WaitGroup will reach zero when all Go routines return.
func (Factory[T]) For ¶
func (f Factory[T]) For(fn func(t T))
ForIdx calls fn sequentially for each value Iter. This does not reset the iterator.
func (Factory[T]) ForIdx ¶
ForIdx calls fn sequentially for each value Iter. This does not reset the iterator.
type Iter ¶
Iter interface allows for a standard set of tools for iterating over a collection.
func Seek ¶
Seek calls fn sequentially for each value Iter returns until Done is true. This does not reset the iterator.
Example ¶
s := &sliceIter[int]{ Slice: []int{3, 1, 4, 1, 5, 9}, } fn := func(i int) bool { return i == 4 } it := liter.Seek[int](s, fn) v, _ := it.Cur() fmt.Println("Value:", v, "Idx:", it.Idx())
Output: Value: 4 Idx: 2
type Reducer ¶
Reducer aggregates a value against every elemnt in the iterator.
type Starter ¶
Starter is an optional interface that Iter can implement to return to the start of the iteration.
type Wrapper ¶
Wrapper provides useful methods that can be applied to any List.
func (Wrapper[T]) Channel ¶
Channel creates a chan with size buf and places each value from Iter on the channel until Done is true. This does not reset the iterator.
func (Wrapper[T]) Concurrent ¶
Concurrent calls fn in a Go routine for each value Iter returns until Done is true. The returned WaitGroup will reach zero when all Go routines return. This does not reset the iterator.
func (Wrapper[T]) For ¶
func (w Wrapper[T]) For(fn func(t T))
For calls fn sequentially for each value Iter. This does not reset the iterator.
func (Wrapper[T]) ForIdx ¶
For calls fn sequentially for each value Iter. This does not reset the iterator.