Documentation ¶
Index ¶
- Variables
- func All[Elem any](iterable Iterable[Elem], fn func(Elem) (bool, error)) (bool, error)
- func Any[Elem any](iterable Iterable[Elem], fn func(Elem) (bool, error)) (bool, error)
- func Each[Elem any](iterable Iterable[Elem], fn func(Elem) error) error
- func Find[Elem any](iterable Iterable[Elem], fn func(Elem) (bool, error)) (item Elem, err error)
- func FindLast[Elem any](iterable Iterable[Elem], fn func(Elem) (bool, error)) (item Elem, err error)
- func First[K, V, Elem any](c Collection[K, V, Elem]) (first Elem, err error)
- func Last[K, V, Elem any](c Collection[K, V, Elem]) (last Elem, err error)
- func Max[V, Elem any](iterable Iterable[Elem], comparator func(V, V) CompareResult) (max Elem, err error)
- func Min[V, Elem any](iterable Iterable[Elem], comparator func(V, V) CompareResult) (min Elem, err error)
- type Collection
- func GroupBy[K, V, Elem any](c Collection[K, V, Elem], gfactory func() Collection[K, V, Elem], ...) (r Collection[K, V, Elem], err error)
- func OrderBy[K, V, Elem any](c Collection[K, V, Elem], comparator func(V, V) CompareResult) (r Collection[K, V, Elem])
- func Select[K, V, Elem, NewK, NewV, NewElem any](c Collection[K, V, Elem], n Collection[NewK, NewV, NewElem], ...) (Collection[NewK, NewV, NewElem], error)
- func Skip[K, V any, Elem any](c Collection[K, V, Elem], n int) (r Collection[K, V, Elem])
- func Take[K, V any, Elem any](c Collection[K, V, Elem], n int) (r Collection[K, V, Elem])
- func Where[K, V, Elem any](c Collection[K, V, Elem], fn func(Elem) (bool, error)) (r Collection[K, V, Elem], err error)
- type Comparable
- type CompareResult
- type Gettable
- type Iterable
- type Iterator
- type LTGTConstraint
- type Ptr
- type Settable
- type Sizable
- type Slicable
Constants ¶
This section is empty.
Variables ¶
var ErrNotComparable = errors.New("collection not comparable")
var ErrNotFound = errors.New("item not found")
Functions ¶
func Each ¶ added in v0.0.12
Each iterates over the collection and calls the given function for each item.
func Find ¶ added in v0.0.10
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.
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 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
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
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.
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 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.