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(yield func(T) bool)
- func (w Wrapper[T]) Seq2(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 Nested ¶
Nested takes an Iter of keys and a function to use a key to get an Iter of values. It iterates over the values calling the Lookup each time the Vals iterator is done until the Keys are done. Nested itself fulfills Iter[Val].
func (*Nested[Key, Val]) Cur ¶
Cur fulfills Iter. It returns the current value and a bool indicating if the iterator is done.
type Reducer ¶
Reducer aggregates a value against every elemnt in the iterator.
func Max ¶
func Max[N constraints.Ordered, T any](fn func(T) N) Reducer[N, T]
Max value in the iter. The fn argument is used to convert to an ordered value. For instance if T is a struct, fn could return one of the fields.
func Min ¶
func Min[N constraints.Ordered, T any](fn func(T) N) Reducer[N, T]
Min value in the iter. The fn argument is used to convert to an ordered value. For instance if T is a struct, fn could return one of the fields.
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.
func (Wrapper[T]) Pop ¶
func (w Wrapper[T]) Pop() T
Pop returns the current value of iterator and if it is not done, calls Next.
func (Wrapper[T]) Seek ¶
Seek calls fn sequentially for each value Iter returns until Done is true. This does not reset the iterator.