liter

package
v0.0.0-...-157c9c8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Channel

func Channel[T any](i Iter[T], buf int) <-chan T

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

func Concurrent[T any](i Iter[T], fn func(t T, idx int)) *sync.WaitGroup

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

func For[T any](i Iter[T], fn func(t T))

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

func ForIdx

func ForIdx[T any](i Iter[T], fn func(t T, idx int)) int

For calls fn sequentially for each value Iter. This does not reset the iterator.

func Pop

func Pop[T any](i Iter[T]) T

Pop returns the current value of iterator and if it is not done, calls Next.

Types

type Factory

type Factory[T any] func() (iter Iter[T], t T, done bool)

Factory creates an iterator.

func (Factory[T]) Channel

func (f Factory[T]) Channel(buf int) <-chan T

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

func (f Factory[T]) Concurrent(fn func(t T, idx int)) *sync.WaitGroup

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

func (f Factory[T]) ForIdx(fn func(t T, idx int)) int

ForIdx calls fn sequentially for each value Iter. This does not reset the iterator.

func (Factory[T]) Seek

func (f Factory[T]) Seek(fn func(t T) bool) Iter[T]

Seek creates a new Iter from the factory and calls fn sequentially for each value Iter returns until Done is true.

func (Factory[T]) Wrap

func (f Factory[T]) Wrap() (iter Wrapper[T], t T, done bool)

type Iter

type Iter[T any] interface {
	Next() (t T, done bool)
	Cur() (t T, done bool)
	Done() bool
	Idx() int
}

Iter interface allows for a standard set of tools for iterating over a collection.

func Seek

func Seek[T any](i Iter[T], fn func(t T) bool) Iter[T]

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

type Nested[Key, Val any] struct {
	Keys   Iter[Key]
	Vals   Iter[Val]
	Lookup func(Key) Iter[Val]
	I      int
}

func NewNested

func NewNested[Key, Val any](keys Iter[Key], lookup func(Key) Iter[Val]) *Nested[Key, Val]

func (*Nested[Key, Val]) Cur

func (n *Nested[Key, Val]) Cur() (val Val, done bool)

func (*Nested[Key, Val]) Done

func (n *Nested[Key, Val]) Done() bool

func (*Nested[Key, Val]) Idx

func (n *Nested[Key, Val]) Idx() int

func (*Nested[Key, Val]) Next

func (n *Nested[Key, Val]) Next() (val Val, done bool)

func (*Nested[Key, Val]) Wrap

func (n *Nested[Key, Val]) Wrap() Wrapper[Val]

type Reducer

type Reducer[A, T any] func(aggregate A, element T, idx int) A

Reducer aggregates a value against every elemnt in the iterator.

func Appender

func Appender[T any]() Reducer[[]T, T]

Appender creates a reducer that appends to a slice.

func Max

func Max[N constraints.Ordered, T any](fn func(T) N) Reducer[N, T]

func Min

func Min[N constraints.Ordered, T any](fn func(T) N) Reducer[N, T]

func (Reducer[A, T]) Factory

func (r Reducer[A, T]) Factory(aggregate A, f Factory[T]) A

Factory runs the Reducer against an Iterator generated from the given Factory.

func (Reducer[A, T]) Iter

func (r Reducer[A, T]) Iter(aggregate A, i Iter[T]) A

Iter runs the Reducer against an Iterator.

type Starter

type Starter[T any] interface {
	Start() (t T, done bool)
}

Starter is an optional interface that Iter can implement to return to the start of the iteration.

type Wrapper

type Wrapper[T any] struct {
	Iter[T]
}

Wrapper provides useful methods that can be applied to any List.

func Wrap

func Wrap[T any](i Iter[T]) Wrapper[T]

Wrap a Iter. Also checks that the underlying Iter is not itself a Wrapper.

func (Wrapper[T]) Channel

func (w Wrapper[T]) Channel(buf int) <-chan T

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

func (w Wrapper[T]) Concurrent(fn func(t T, idx int)) *sync.WaitGroup

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

func (w Wrapper[T]) ForIdx(fn func(t T, idx int)) int

For calls fn sequentially for each value Iter. This does not reset the iterator.

func (Wrapper[T]) Pop

func (w Wrapper[T]) Pop() T

func (Wrapper[T]) Seek

func (w Wrapper[T]) Seek(fn func(t T) bool) Iter[T]

Seek calls fn sequentially for each value Iter returns until Done is true. This does not reset the iterator.

func (Wrapper[T]) Seq

func (w Wrapper[T]) Seq() func(yield func(T) bool)

func (Wrapper[T]) Seq2

func (w Wrapper[T]) Seq2() func(yield func(int, T) bool)

func (Wrapper[T]) Wrapped

func (w Wrapper[T]) Wrapped() any

Wrapped fulfills upgrade.Wrapper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL