iterator

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package iterator provides a way to iterate over values stored in containers. note: 1. Full feature iterator is complicated, this package is just a experiment to explore how iterators could work in Go. 2. The functionality of this package is very simple and limited, may not meet the actual dev needs. 3. It is currently under development, unstable, and will not be completed for some time in the future. So, based on above factors, you may not use it in production. but, anyone is welcome to improve it. Hope that Go can support iterator in future. see https://github.com/golang/go/discussions/54245 and https://github.com/golang/go/discussions/56413

Package iterator provides a way to iterate over values stored in containers. note: 1. Full feature iterator is complicated, this package is just a experiment to explore how iterators could work in Go. 2. The functionality of this package is very simple and limited, may not meet the actual dev needs. 3. It is currently under development, unstable, and will not be completed for some time in the future. So, based on above factors, you may not use it in production. but, anyone is welcome to improve it. Hope that Go can support iterator in future. see https://github.com/golang/go/discussions/54245 and https://github.com/golang/go/discussions/56413

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reduce added in v2.1.13

func Reduce[T any, U any](iter Iterator[T], initial U, reducer func(U, T) U) U

Reduce reduces iter to a single value using the reduction function reducer

func ToChannel added in v2.1.13

func ToChannel[T any](ctx context.Context, iter Iterator[T], buffer int) <-chan T

ToChannel create a new goroutine to pull items from the channel iterator to the returned channel.

func ToSlice

func ToSlice[T any](iter Iterator[T]) []T

Types

type ChannelIterator added in v2.3.0

type ChannelIterator[T any] struct {
	// contains filtered or unexported fields
}

func FromChannel added in v2.1.13

func FromChannel[T any](channel <-chan T) *ChannelIterator[T]

FromChannel creates an iterator which returns items received from the provided channel. The iteration continues until the channel is closed.

func (*ChannelIterator[T]) HasNext added in v2.3.0

func (iter *ChannelIterator[T]) HasNext() bool

func (*ChannelIterator[T]) Next added in v2.3.0

func (iter *ChannelIterator[T]) Next() (T, bool)

type DeleteIterator

type DeleteIterator[T any] interface {
	Iterator[T]

	// Delete deletes the current iterator element;
	// that is, the one returned by the last call to Next.
	// Delete should panic if called before Next or after
	// Next returns false.
	Delete()
}

DeleteIter is an Iter that implements a Delete method.

type Iterator

type Iterator[T any] interface {
	// Next checks if there is a next value in the iteration or not
	HasNext() bool
	// Next returns the next value in the iteration if there is one,
	// and reports whether the returned value is valid.
	// Once Next returns ok==false, the iteration is over,
	// and all subsequent calls will return ok==false.
	Next() (item T, ok bool)
}

Iterator supports iterating over a sequence of values of type `E`.

func Filter

func Filter[T any](iter Iterator[T], predicateFunc func(item T) bool) Iterator[T]

Filter creates a new iterator that returns only the items that pass specified predicate function.

func Join added in v2.1.13

func Join[T any](iters ...Iterator[T]) Iterator[T]

Join creates an iterator that join all elements of iters[0], then all elements of iters[1] and so on.

func Map

func Map[T any, U any](iter Iterator[T], iteratee func(item T) U) Iterator[U]

Map creates a new iterator which applies a function to all items of input iterator.

func Take added in v2.1.13

func Take[T any](it Iterator[T], num int) Iterator[T]

type PrevIterator

type PrevIterator[T any] interface {
	Iterator[T]

	// Prev moves the iterator to the previous position.
	// After calling Prev, Next will return the value at
	// that position in the container. For example, after
	//   it.Next() returning (v, true)
	//   it.Prev()
	// another call to it.Next will again return (v, true).
	// Calling Prev before calling Next may panic.
	// Calling Prev after Next returns false will move
	// to the last element, or, if there are no elements,
	// to the iterator's initial state.
	Prev()
}

PrevIterator is an iterator with a Prev method.

type RangeIterator added in v2.3.0

type RangeIterator[T constraints.Integer | constraints.Float] struct {
	// contains filtered or unexported fields
}

func FromRange

func FromRange[T constraints.Integer | constraints.Float](start, end, step T) *RangeIterator[T]

FromRange creates a iterator which returns the numeric range between start inclusive and end exclusive by the step size. start should be less than end, step shoud be positive.

func (*RangeIterator[T]) HasNext added in v2.3.0

func (iter *RangeIterator[T]) HasNext() bool

func (*RangeIterator[T]) Next added in v2.3.0

func (iter *RangeIterator[T]) Next() (T, bool)

func (*RangeIterator[T]) Reset added in v2.3.0

func (iter *RangeIterator[T]) Reset()

type ResettableIterator added in v2.3.0

type ResettableIterator[T any] interface {
	Iterator[T]
	// Reset allows for the iteration process over a sequence to be restarted from the beginning.
	// It enables reusing the iterator for multiple traversals without needing to recreate it.
	Reset()
}

ResettableIterator supports to reset the iterator

type SetIterator

type SetIterator[T any] interface {
	Iterator[T]

	// Set replaces the current iterator element with v.
	// Set should panic if called before Next or after
	// Next returns false.
	Set(v T)
}

SetIterator is an Iter that implements a Set method.

type SliceIterator added in v2.3.0

type SliceIterator[T any] struct {
	// contains filtered or unexported fields
}

func FromSlice

func FromSlice[T any](slice []T) *SliceIterator[T]

FromSlice returns an iterator over a slice of data.

func (*SliceIterator[T]) HasNext added in v2.3.0

func (iter *SliceIterator[T]) HasNext() bool

func (*SliceIterator[T]) Next added in v2.3.0

func (iter *SliceIterator[T]) Next() (T, bool)

func (*SliceIterator[T]) Prev added in v2.3.0

func (iter *SliceIterator[T]) Prev()

Prev implements PrevIterator.

func (*SliceIterator[T]) Reset added in v2.3.0

func (iter *SliceIterator[T]) Reset()

func (*SliceIterator[T]) Set added in v2.3.0

func (iter *SliceIterator[T]) Set(value T)

Set implements SetIterator.

type StopIterator

type StopIterator[T any] interface {
	Iterator[T]

	// Stop indicates that the iterator will no longer be used.
	// After a call to Stop, future calls to Next may panic.
	// Stop may be called multiple times;
	// all calls after the first will have no effect.
	Stop()
}

StopIterator is an interface for stopping Iterator.

Jump to

Keyboard shortcuts

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