iterator

package
v2.0.0-...-fb11600 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2023 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

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

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 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 FromChannel

func FromChannel[T any](channel <-chan T) Iterator[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 FromRange

func FromRange[T constraints.Integer | constraints.Float](start, end, step T) Iterator[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 FromSlice

func FromSlice[T any](slice []T) Iterator[T]

FromSlice returns an iterator over a slice of data.

func Join

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

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 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 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