generics

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 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 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.

func Reverse added in v0.0.6

func Reverse[T LTGTConstraint](slice []T)

Reverse sorts the given slice in descending order. The type parameter must be a native number type.

func SliceMap

func SliceMap[T any, U any](slice []T, fn func(T) (U, error)) (result []U, err error)

SliceMap returns a new slice with the results of applying the given function to each element of the given slice.

func Sort added in v0.0.3

func Sort[T LTGTConstraint](slice []T)

Sort sorts the given slice in ascending order. The type parameter must be a native number type.

func SortF added in v0.0.3

func SortF[T any](slice []T, fn func(T, T) (bool, error)) error

SortF sorts the given slice using the given function.

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]

	// 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 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])

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

type Iterator added in v0.0.6

type Iterator[T any] struct {
	// 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]) (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]) Done added in v0.0.8

func (it *Iterator[T]) Done() <-chan struct{}

Done returns a channel that is closed when the iterator is done. used by Iterable.IterHandler to stop iteration.

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.

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 SafeMap

type SafeMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SafeMap is a thread-safe map.

func NewSafeMap

func NewSafeMap[K comparable, V any](items ...Tuple[K, V]) *SafeMap[K, V]

NewSafeMap creates a new SafeMap.

func (*SafeMap[K, V]) AppendElem added in v0.0.10

func (m *SafeMap[K, V]) AppendElem(elem Tuple[K, V])

AppendElem appends an element to the map.

func (*SafeMap[K, V]) AsCollection added in v0.0.9

func (m *SafeMap[K, V]) AsCollection() Collection[K, V, Tuple[K, V]]

func (*SafeMap[K, V]) AsIterable added in v0.0.9

func (m *SafeMap[K, V]) AsIterable() Iterable[Tuple[K, V]]

func (*SafeMap[K, V]) Cap added in v0.0.6

func (m *SafeMap[K, V]) Cap() int

Cap returns the capacity of the map, which is equal to the length.

func (*SafeMap[K, V]) Clear

func (m *SafeMap[K, V]) Clear()

Clear clears the map.

func (*SafeMap[K, V]) Clone added in v0.0.6

func (m *SafeMap[K, V]) Clone() Collection[K, V, Tuple[K, V]]

Clone returns a copy of the map.

func (*SafeMap[K, V]) Delete

func (m *SafeMap[K, V]) Delete(key K)

Delete deletes a value from the map.

func (*SafeMap[K, V]) Factory added in v0.0.10

func (m *SafeMap[K, V]) Factory() Collection[K, V, Tuple[K, V]]

Factory returns a new instance of the map.

func (*SafeMap[K, V]) Get

func (m *SafeMap[K, V]) Get(key K) V

Get gets a value from the map. Panics if the key does not exist.

func (*SafeMap[K, V]) GetE added in v0.0.6

func (m *SafeMap[K, V]) GetE(key K) (V, bool)

GetE gets a value from the map.

func (*SafeMap[K, V]) HasKey

func (m *SafeMap[K, V]) HasKey(key K) bool

HasKey checks if the map has the key.

func (*SafeMap[K, V]) IsEmpty

func (m *SafeMap[K, V]) IsEmpty() bool

IsEmpty checks if the map is empty.

func (*SafeMap[K, V]) Iter

func (m *SafeMap[K, V]) Iter() *Iterator[Tuple[K, V]]

func (*SafeMap[K, V]) IterHandler added in v0.0.8

func (m *SafeMap[K, V]) IterHandler(iter *Iterator[Tuple[K, V]])

func (*SafeMap[K, V]) Keys

func (m *SafeMap[K, V]) Keys() []K

Keys returns all keys in the map.

func (*SafeMap[K, V]) Len

func (m *SafeMap[K, V]) Len() int

Len returns the length of the map.

func (*SafeMap[K, V]) Set

func (m *SafeMap[K, V]) Set(key K, val V)

Set sets a value in the map.

func (*SafeMap[K, V]) SetElem added in v0.0.9

func (m *SafeMap[K, V]) SetElem(elem Tuple[K, V])

SetElem sets a value in the map.

func (*SafeMap[K, V]) Values

func (m *SafeMap[K, V]) Values() []V

Values returns all values in the map.

type SafeSlice added in v0.0.6

type SafeSlice[V any] struct {
	// contains filtered or unexported fields
}

SafeSlice is a thread-safe slice.

It doesn't check out of bounds access.

func NewSafeSlice added in v0.0.6

func NewSafeSlice[V any](items ...V) *SafeSlice[V]

NewSafeSlice creates a new SafeSlice.

func NewSafeSliceN added in v0.0.6

func NewSafeSliceN[V any](s, n int) *SafeSlice[V]

NewSafeSliceN creates a new SafeSlice with size and capacity of n.

func (*SafeSlice[V]) Append added in v0.0.6

func (s *SafeSlice[V]) Append(item V)

Append appends a value to the slice.

func (*SafeSlice[V]) AppendElem added in v0.0.10

func (s *SafeSlice[V]) AppendElem(elem Tuple[int, V])

AppendElem appends a value to the slice.

func (*SafeSlice[V]) AppendSafeSlice added in v0.0.6

func (s *SafeSlice[V]) AppendSafeSlice(other *SafeSlice[V])

AppendSafeSlice appends a SafeSlice to the slice.

func (*SafeSlice[V]) AppendSlice added in v0.0.6

func (s *SafeSlice[V]) AppendSlice(slice []V)

AppendSlice appends a slice to the slice.

func (*SafeSlice[V]) AsCollection added in v0.0.9

func (s *SafeSlice[V]) AsCollection() Collection[int, V, Tuple[int, V]]

func (*SafeSlice[V]) AsIterable added in v0.0.9

func (s *SafeSlice[V]) AsIterable() Iterable[Tuple[int, V]]

func (*SafeSlice[V]) Cap added in v0.0.6

func (s *SafeSlice[V]) Cap() int

Cap returns the capacity of the slice.

func (*SafeSlice[V]) Clear added in v0.0.6

func (s *SafeSlice[V]) Clear()

Clear clears the slice.

func (*SafeSlice[V]) Clone added in v0.0.6

func (s *SafeSlice[V]) Clone() Collection[int, V, Tuple[int, V]]

Clone returns a clone of the slice. Same as Values.

func (*SafeSlice[V]) Compare added in v0.0.6

func (s *SafeSlice[V]) Compare(i, j Tuple[int, V], comp func(V, V) CompareResult) CompareResult

Compare compares two slice items.

func (*SafeSlice[V]) Delete added in v0.0.6

func (s *SafeSlice[V]) Delete(i int)

Delete deletes a value at given index from the slice.

func (*SafeSlice[V]) Factory added in v0.0.10

func (s *SafeSlice[V]) Factory() Collection[int, V, Tuple[int, V]]

Factory returns a new SafeSlice.

func (*SafeSlice[V]) Get added in v0.0.6

func (s *SafeSlice[V]) Get(i int) V

Get gets a value from the slice.

func (*SafeSlice[V]) IsEmpty added in v0.0.6

func (s *SafeSlice[V]) IsEmpty() bool

IsEmpty returns true if the slice is empty.

func (*SafeSlice[V]) Iter added in v0.0.6

func (s *SafeSlice[V]) Iter() *Iterator[Tuple[int, V]]

func (*SafeSlice[V]) IterHandler added in v0.0.8

func (s *SafeSlice[V]) IterHandler(iter *Iterator[Tuple[int, V]])

func (*SafeSlice[V]) Len added in v0.0.6

func (s *SafeSlice[V]) Len() int

Len returns the length of the slice.

func (*SafeSlice[V]) Set added in v0.0.6

func (s *SafeSlice[V]) Set(i int, item V)

Set sets a value in the slice.

func (*SafeSlice[V]) SetElem added in v0.0.9

func (s *SafeSlice[V]) SetElem(item Tuple[int, V])

SetElem sets a value in the slice.

func (*SafeSlice[V]) Slice added in v0.0.10

func (s *SafeSlice[V]) Slice(start, end int) *SafeSlice[V]

Slice returns a new SafeSlice with the elements from start to end-1.

func (*SafeSlice[V]) Values added in v0.0.6

func (s *SafeSlice[V]) Values() []V

Values returns all values in the slice. Same as Clone.

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.

type Tuple

type Tuple[T any, U any] struct {
	First  T
	Second U
}

func NewTuple

func NewTuple[T any, U any](first T, second U) Tuple[T, U]

NewTuple creates a new Tuple.

func (Tuple[T, U]) Key added in v0.0.6

func (t Tuple[T, U]) Key() T

Key returns the first value of the tuple.

func (Tuple[T, U]) Value added in v0.0.6

func (t Tuple[T, U]) Value() U

Value returns the second value of the tuple.

Jump to

Keyboard shortcuts

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