enumerable

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: MIT Imports: 2 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForEach

func ForEach[T any](source Enumerable[T], action func(item T)) error

ForEach iterates over the given source `Enumerable` performing the given action on each item. It resets the source `Enumerable` on completion.

func OnEach

func OnEach[T any](source Enumerable[T], action func()) error

OnEach iterates over the given source `Enumerable` performing the given action for each item yielded. It resets the source `Enumerable` on completion.

func TryGetFirst

func TryGetFirst[T any](source Enumerable[T]) (T, bool, error)

TryGetFirst returns the first element yielded from the given source along with true. If no items are yielded by the source, then false with be returned. Any errors generated during enumeration will be yielded instead of a value.

Types

type Concatenation added in v0.3.0

type Concatenation[T any] interface {
	Enumerable[T]
	// Append appends a new source to this concatenation.
	//
	// This may be done after enumeration has begun.
	Append(Enumerable[T])
}

Concatenation is an extention of the enumerable interface allowing new sources to be added after initial construction.

func Concat

func Concat[T any](sources ...Enumerable[T]) Concatenation[T]

Concat takes zero to many source `Ènumerable`s and stacks them on top of each other, resulting in one enumerable that will iterate through all the values in all of the given sources.

New sources may be added after iteration has begun.

type Enumerable

type Enumerable[T any] interface {
	// Next attempts to evaluate the next item in the enumeration - allowing its
	// exposure via the `Value()` function.
	//
	// It will return false if it has reached the end of the enumerable, and/or an
	// error if one was generated during evaluation.
	Next() (bool, error)

	// Value returns the current item in the enumeration. It does not progress the
	// enumeration, and should be a simple getter.
	//
	// If the previous Next call did not return true, or Next has never been called
	// the behaviour and return value of this function is undefined.
	Value() (T, error)

	// Reset resets the enumerable, allowing for re-iteration.
	Reset()
}

Enumerable represents a set of elements that can be iterated through multiple times.

The enumerable may be a composite of multiple actions that will be lazily executed upon iteration, allowing the enumerable to be constructed out of a complex set of instructions that can be evaluated in a single iteration of the underlying set.

func New

func New[T any](source []T) Enumerable[T]

New creates an `Enumerable` from the given slice.

func Select

func Select[TSource any, TResult any](
	source Enumerable[TSource],
	selector func(TSource) (TResult, error),
) Enumerable[TResult]

Select creates a new `Enumerable` that iterates through each item yielded by the given source and then yields the value returned by the given selector.

func Skip

func Skip[T any](source Enumerable[T], offset uint64) Enumerable[T]

Skip creates an `Enumerable` from the given `Enumerable` and offset. The returned `Enumerable` will skip through items until the number of items yielded from source excedes the give offset.

func Sort

func Sort[T any](source Enumerable[T], less func(T, T) bool, capacity int) Enumerable[T]

Sort creates an `Enumerable` from the given `Enumerable`, using the given less function to determine as to whether an item is less than the other in in terms of order.

The returned `Enumerable` will enumerate the entire source enumerable on the first `Next` call, but will not enumerate it again unless reset.

func Take

func Take[T any](source Enumerable[T], limit uint64) Enumerable[T]

Take creates an `Enumerable` from the given `Enumerable` and limit. The returned `Enumerable` will restrict the maximum number of items yielded to the given limit.

func Where

func Where[T any](source Enumerable[T], predicate func(T) (bool, error)) Enumerable[T]

Where creates an `Enumerable` from the given `Enumerable` and predicate. Items in the source `Enumerable` must return true when passed into the predicate in order to be yielded from the returned `Enumerable`.

type Queue added in v0.3.0

type Queue[T any] interface {
	Enumerable[T]
	// Put adds an item to the queue.
	Put(T) error
	// Size returns the current length of the backing array.
	//
	// This may include empty space where yield items previously resided.
	// Useful for testing and debugging.
	Size() int
}

Queue is an extention of the enumerable interface allowing individual items to be added into the enumerable.

Added items will be yielded in a FIFO order. Items may be added after enumeration has begun.

func NewQueue added in v0.3.0

func NewQueue[T any]() Queue[T]

NewQueue creates an empty FIFO queue.

It is implemented using a dynamically sized ring-buffer.

type Socket added in v0.3.0

type Socket[T any] interface {
	Enumerable[T]
	// SetSource sets the source to this enumerable.
	//
	// This may be done after enumeration has begun.
	SetSource(Enumerable[T])
}

Socket is an extention of the enumerable interface allowing the source to be replaced after initial construction.

func NewSocket added in v0.3.0

func NewSocket[T any]() Socket[T]

NewSocket creates a new Socket enumerable with no initial source.

The source may be set, and even swapped out, later during its lifetime. If enumeration begins before a source has been set it will behave as if empty. Reseting the Socket will reset the source if there is one, and then remove it as the source of this Socket.

Jump to

Keyboard shortcuts

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