Iterators

package
v0.2.26 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder is a struct that allows building a collection of elements of type T in a procedural manner. It is used to build a collection of elements by appending elements to a buffer and then creating an iterator over the buffer.

func (*Builder[T]) Append

func (b *Builder[T]) Append(element T)

Append is a method of the Builder type that appends an element to the buffer of elements to be built.

Parameters:

  • element: The element to append to the buffer.

func (*Builder[T]) Build

func (b *Builder[T]) Build() Iterater[T]

Build is a method of the Builder type that creates an iterator over the buffer of elements. Finally, the buffer is cleared.

Returns:

  • Iterater[T]: A pointer to a new iterator over the buffer of elements.

func (*Builder[T]) Clear

func (b *Builder[T]) Clear()

Clear is a method of the Builder type that removes all elements from the buffer.

type GenericIterator

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

GenericIterator is a struct that allows iterating over a slice of elements of any type.

func (*GenericIterator[T]) Next

func (iter *GenericIterator[T]) Next() (hasNext bool)

Next is a method of the GenericIterator type that advances the iterator to the next element in the collection and returns true if there is a next element, otherwise false.

Returns:

  • bool: True if there is a next element, otherwise false.

func (*GenericIterator[T]) Restart

func (iter *GenericIterator[T]) Restart()

Restart is a method of the GenericIterator type that resets the iterator to the beginning of the collection.

func (*GenericIterator[T]) Value

func (iter *GenericIterator[T]) Value() (T, error)

Value is a method of the GenericIterator type that returns the current element in the collection. It should be called after Next to get the current element.

Panics with *ErrCallFailed if the iterator is exhausted, if it is called before the iterator is initialized, or if it is called before Next.

Returns:

  • T: The current element in the collection.

func (*GenericIterator[T]) ValueNoErr

func (iter *GenericIterator[T]) ValueNoErr() T

ValueNoErr is a method of the GenericIterator type that returns the current element in the collection without an error. It should be called after Next to get the current element.

Panics with *ErrCallFailed if the iterator is exhausted, if it is called before the iterator is initialized, or if it is called before Next.

Returns:

  • T: The current element in the collection.

type Iterable

type Iterable[T any] interface {
	Iterator() Iterater[T]
}

Iterable is an interface that defines a method to get an iterator over a collection of elements of type T. It is implemented by data structures that can be iterated over.

type Iterater

type Iterater[T any] interface {
	// The Next method advances the iterator to the next element in the collection.
	// It returns true if there is a next element, otherwise false.
	Next() bool

	// The Value method returns the current element in the collection.
	// It should be called after Next to get the current element.
	//
	// If the iterator is exhausted, it will panic.
	Value() (T, error)

	// The Restart method resets the iterator to the beginning of the collection.
	Restart()
}

Iterater is an interface that defines methods for an iterator over a collection of elements of type T. It includes methods to get the next element, get the current element, and restart the iterator.

func IteratorFromIterator

func IteratorFromIterator[E, T any](source Iterater[E], f func(E) Iterater[T]) (Iterater[T], error)

IteratorFromIterator creates a new iterator over a collection of iterators of type Iterater[T]. It uses the input iterator to iterate over the collection of iterators and return the elements from each iterator in turn.

Parameters:

  • source: The iterator over the collection of iterators to iterate over.
  • f: The transition function that takes an element of type E and returns an iterator

Return:

  • Iterater[T]: A pointer to a new iterator over the collection of iterators.

func IteratorFromSlice

func IteratorFromSlice[T any](values []T) Iterater[T]

IteratorFromSlice creates a new iterator over a slice of elements of type T. It creates a shallow copy of the input slice to minimize side effects.

Parameters:

  • values: The slice of elements to iterate over.

Return:

  • Iterater[T]: A pointer to a new iterator over the given slice of elements.

func IteratorFromSlicer

func IteratorFromSlicer[T any](slicer itf.Slicer[T]) Iterater[T]

IteratorFromSlicer creates a new iterator over a data structure that implements the Slicer interface. It uses the Slice method of the data structure to get the slice of elements to iterate over.

Parameters:

  • slicer: The data structure that implements the Slicer interface.

Return:

  • Iterater[T]: A pointer to a new iterator over the slice of elements returned by the Slice method of the input slicer.

func IteratorFromValues

func IteratorFromValues[T any](values ...T) Iterater[T]

IteratorFromValues creates a new iterator over a variadic list of elements of type T. It creates a shallow copy of the input variadic list to minimize side effects.

Parameters:

  • values: The variadic list of elements to iterate over.

Return:

  • Iterater[T]: A pointer to a new iterator over the given variadic list of elements.

type ProceduralIterator

type ProceduralIterator[E, T any] struct {
	// contains filtered or unexported fields
}

ProceduralIterator is a struct that allows iterating over a collection of iterators of type Iterater[T]. The major difference between this and the GenericIterator is that this iterator is designed to iterate over a collection of elements in a progressive manner; reducing the need to store the entire collection in memory.

func (*ProceduralIterator[E, T]) Next

func (iter *ProceduralIterator[E, T]) Next() bool

Next is a method of the ProceduralIterator type that advances the iterator to the next element in the collection and returns true if there is a next element, otherwise false.

Panics with *ErrCallFailed if the iterator is exhausted or if it is called before the iterator is initialized.

Returns:

  • bool: True if there is a next element, otherwise false.

func (*ProceduralIterator[E, T]) Restart

func (iter *ProceduralIterator[E, T]) Restart()

Restart is a method of the ProceduralIterator type that resets the iterator to the beginning of the collection.

func (*ProceduralIterator[E, T]) Value

func (iter *ProceduralIterator[E, T]) Value() (T, error)

Value is a method of the ProceduralIterator type that returns the current element in the collection. It should be called after Next to get the current element.

Panics with *ErrCallFailed if the iterator is exhausted, if it is called before the iterator is initialized, or if it is called before Next.

Returns:

  • T: The current element in the collection.

func (*ProceduralIterator[E, T]) ValueNoErr

func (iter *ProceduralIterator[E, T]) ValueNoErr() T

ValueNoErr is a method of the ProceduralIterator type that returns the current element in the collection without an error. It should be called after Next to get the current element.

Panics with *ErrCallFailed if the iterator is exhausted, if it is called before the iterator is initialized, or if it is called before Next.

Returns:

  • T: The current element in the collection.

Jump to

Keyboard shortcuts

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