generics

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotComparable = errors.New("collection not comparable")
View Source
var ErrNotFound = errors.New("item not found")

Functions

func All added in v0.0.6

func All[Elem any](iterable Iterable[Elem], fn func(Elem) (bool, error)) (bool, error)

All returns true if all items in the collection match the predicate.

func Any added in v0.0.6

func Any[Elem any](iterable Iterable[Elem], fn func(Elem) (bool, error)) (bool, error)

Any returns true if any item in the collection matches the predicate.

func Each added in v0.0.12

func Each[Elem any](iterable Iterable[Elem], fn func(Elem) error) error

Each iterates over the collection and calls the given function for each item.

func Find added in v0.0.10

func Find[Elem any](iterable Iterable[Elem], fn func(Elem) (bool, error)) (item Elem, err error)

Find returns the first item in the collection that matches the predicate.

func FindLast added in v0.0.10

func FindLast[Elem any](iterable Iterable[Elem], fn func(Elem) (bool, error)) (item Elem, err error)

FindLast returns the last item in the collection that matches the predicate.

func First added in v0.0.6

func First[K, V, Elem any](c Collection[K, V, Elem]) (first Elem, err error)

First returns the first item in the collection.

func Last added in v0.0.6

func Last[K, V, Elem any](c Collection[K, V, Elem]) (last Elem, err error)

Last returns the last item in the collection.

func Max added in v0.0.6

func Max[V, Elem any](iterable Iterable[Elem], comparator func(V, V) CompareResult) (max Elem, err error)

Max returns the maximum item in the collection.

func Min added in v0.0.6

func Min[V, Elem any](iterable Iterable[Elem], comparator func(V, V) CompareResult) (min Elem, err error)

Min returns the minimum item in the collection.

Types

type Collection added in v0.0.6

type Collection[K, V, Elem any] interface {
	Sizable
	Gettable[K, V]
	Settable[K, V, Elem]
	Iterable[Elem]

	// Factory returns a new collection of the same type.
	Factory() Collection[K, V, Elem]

	// FactoryFrom returns a new collection of the same type, initialized with the given elements.
	FactoryFrom([]V) Collection[K, V, Elem]

	// Clone returns a copy of the collection.
	Clone() Collection[K, V, Elem]

	// AsCollection returns the collection as a Collection[K, V, Elem].
	AsCollection() Collection[K, V, Elem]
}

Collection is a generic interface for collections. It is implemented by all collections in this package.

The generic parameters are:

	K: the key type
	V: the value type
 Elem: iterator element type, usually a Tuple[K, V]

func GroupBy added in v0.0.15

func GroupBy[K, V, Elem any](
	c Collection[K, V, Elem],
	gfactory func() Collection[K, V, Elem],
	keySelector func(Elem) (K, error),
) (r Collection[K, V, Elem], err error)

GroupBy returns a new collection grouped by the given key selector.

returns a slice of collections, each one containing the elements that match the key.

func OrderBy added in v0.0.9

func OrderBy[K, V, Elem any](c Collection[K, V, Elem], comparator func(V, V) CompareResult) (r Collection[K, V, Elem])

OrderBy returns a new collection ordered by the given comparator.

func Select added in v0.0.10

func Select[K, V, Elem, NewK, NewV, NewElem any](
	c Collection[K, V, Elem],
	n Collection[NewK, NewV, NewElem],
	fn func(Elem) (NewElem, error)) (Collection[NewK, NewV, NewElem], error)

Select returns a new collection with the items transformed by the given function. The n collection is the mapped collection, and it's preferred to be empty.

func Skip added in v0.0.9

func Skip[K, V any, Elem any](c Collection[K, V, Elem], n int) (r Collection[K, V, Elem])

Skip returns a new collection with the first n items skipped.

func Take added in v0.0.9

func Take[K, V any, Elem any](c Collection[K, V, Elem], n int) (r Collection[K, V, Elem])

Take returns a new collection with the first n items.

func Where added in v0.0.10

func Where[K, V, Elem any](c Collection[K, V, Elem], fn func(Elem) (bool, error)) (r Collection[K, V, Elem], err error)

Where returns a new collection with the items that match the predicate.

type Comparable added in v0.0.6

type Comparable[V, Iter any] interface {
	Compare(Iter, Iter, func(V, V) CompareResult) CompareResult
}

type CompareResult added in v0.0.6

type CompareResult int
const (
	LessThan CompareResult = iota
	EqualTo
	GreaterThan
)

func NumericComparator added in v0.0.6

func NumericComparator[V LTGTConstraint](a, b V) CompareResult

NumericComparator is a comparator for numeric types included in LTGTConstraint. It's meant to be used as compare handler in Comparable.Compare.

func StringComparator added in v0.0.6

func StringComparator(a, b string) CompareResult

StringComparator is a comparator for strings. It's meant to be used as compare handler in Comparable.Compare.

type Gettable added in v0.0.6

type Gettable[K, V any] interface {
	Get(K) V
	Values() []V
}

Gettable is a generic interface for collections that support reading.

type Iterable added in v0.0.6

type Iterable[T any] interface {
	// Iter returns an iterator for the collection.
	Iter() *Iterator[T]

	// IterHandler is the iterator handler goroutine.
	IterHandler(*Iterator[T])

	// AsIterable returns the iterator type.
	AsIterable() Iterable[T]
}

type Iterator added in v0.0.6

type Iterator[T any] struct {
	context.Context

	Args []any
	// contains filtered or unexported fields
}

Iterator is a generic iterator used to iterate over Collection. Use Iterator.Close for early loop termination.

func NewIterator added in v0.0.6

func NewIterator[T any](owner Iterable[T], args ...any) (it *Iterator[T])

NewIterator returns a new iterator. Used by owner.Iter().

func (*Iterator[T]) Close added in v0.0.6

func (it *Iterator[T]) Close()

Close stops the iterator. Used to stop iteration early.

func (*Iterator[T]) Error added in v0.0.15

func (it *Iterator[T]) Error() error

Error returns the error on the iterator. Used by Iterable.IterHandler to signal an error.

func (*Iterator[T]) IterationDone added in v0.0.8

func (it *Iterator[T]) IterationDone()

IterationDone closes the iterator channel. Used by Iterable.IterHandler to signal that iteration is done.

func (*Iterator[T]) Next added in v0.0.8

func (it *Iterator[T]) Next() <-chan T

Next returns the next item in the iterator.

Each time Next is called, current item will be returned and goroutine will read the next item. This might be costly for collections that a single iteration is heavy.

func (*Iterator[T]) NextChannel added in v0.0.8

func (it *Iterator[T]) NextChannel() chan<- T

NextChannel returns the receiving channel to be returned by Next(). used by Iterable.IterHandler to send items.

func (*Iterator[T]) SetError added in v0.0.15

func (it *Iterator[T]) SetError(err error)

SetError sets the error on the iterator. Used by Iterable.IterHandler to signal an error.

type LTGTConstraint added in v0.0.3

type LTGTConstraint interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
}

LTGTConstraint is a constraint that requires the type to implement the < and > operators.

type Ptr added in v0.0.15

type Ptr[T any] interface {
	*T
}

type Settable added in v0.0.6

type Settable[K, V, Elem any] interface {
	Set(K, V)
	SetElem(Elem)
	Delete(K)
	Clear()
	AppendElem(Elem)
}

Settable is a generic interface for collections that support writing.

type Sizable added in v0.0.6

type Sizable interface {
	Len() int
	Cap() int
	IsEmpty() bool
}

Sizable is a generic interface for collections that can be sized.

type Slicable added in v0.0.10

type Slicable interface {
	Slice(start, end int) Slicable
}

Slicable is a collection that can be sliced.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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