go.bigb.es/auxilia

v0.2.0
Doc Versions Source

Documentation

Package collect provides generic collection types, lazy iterators, and slice helpers.

Functions

f func Reverse

src
func Reverse[E any](inp []E) []E

Reverse returns a new slice with the elements of inp in reverse order. The input slice is not modified.

f func UniqueComparableOrdered

src
func UniqueComparableOrdered[V comparable](inp []V) []V

UniqueComparableOrdered deduplicates elements of inp, preserving the insertion order of the first occurrence of each value.

Types

T type ListGenerator

src
type ListGenerator[E any] func() (E, bool)

ListGenerator produces values one at a time. It returns (value, true) for each element and (zero, false) when the sequence is exhausted.

T type ListIterator

src
type ListIterator[E any] interface {
	// Map returns an iterator that applies f to each element.
	Map(f func(E) E) ListIterator[E]
	// Filter returns an iterator that yields only elements satisfying pred.
	Filter(pred func(E) bool) ListIterator[E]
	// Sort collects all elements, sorts them with less, and returns a new iterator.
	Sort(less func(E, E) bool) ListIterator[E]
	// Skip returns an iterator that discards the first count elements.
	// Panics if count is negative.
	Skip(count int) ListIterator[E]
	// Limit returns an iterator that yields at most count elements.
	// Panics if count is negative.
	Limit(count int) ListIterator[E]
	// Count exhausts the iterator and returns the number of elements.
	Count() int
	// Apply exhausts the iterator calling f for each element and returns the count.
	Apply(f func(E)) int
	// Collect collects all remaining elements into a slice.
	Collect() []E
	// MapIndex returns an iterator that applies f with the 0-based index to each element.
	MapIndex(f func(int, E) E) ListIterator[E]
	// FilterIndex returns an iterator that yields only elements where pred(index, elem) is true.
	FilterIndex(pred func(int, E) bool) ListIterator[E]
	// Unique returns an iterator that deduplicates elements using the string key returned by fn.
	Unique(fn func(E) string) ListIterator[E]
	// ApplyIndex exhausts the iterator calling f with the 0-based index for each element.
	ApplyIndex(f func(int, E))
	// Materialize materializes the iterator and returns a new iterator over the result.
	Materialize() ListIterator[E]
}

ListIterator is a lazy, pull-based iterator over a sequence of elements. All intermediate operations (Map, Filter, Sort, Skip, Limit) return a new iterator; terminal operations (Count, Apply, Collect) consume it.

f func ConvertList

src
func ConvertList[I any, O any](inp ListIterator[I], cb func(I) O) ListIterator[O]

ConvertList transforms a ListIterator of type I into a ListIterator of type O by applying cb to each element.

f func FromMapKeys

src
func FromMapKeys[K comparable, V any](inp map[K]V) ListIterator[K]

FromMapKeys creates a ListIterator over the keys of a map.

f func FromMapValues

src
func FromMapValues[K comparable, V any](inp map[K]V) ListIterator[V]

FromMapValues creates a ListIterator over the values of a map.

f func FromSlice

src
func FromSlice[E any](inp []E) ListIterator[E]

FromSlice creates a ListIterator that yields the elements of inp in order.

f func FromSliceWith

src
func FromSliceWith[E any, I any](inp []E, cb func(E) I) ListIterator[I]

FromSliceWith creates a ListIterator that maps each element of inp through cb.

f func NewListIterator

src
func NewListIterator[E any](gen ListGenerator[E], maxElements int) ListIterator[E]

NewListIterator creates a ListIterator backed by gen. maxElements is a hint for the expected upper bound of elements (used for pre-allocation); pass -1 if unknown.

T type MapGenerator

src
type MapGenerator[K comparable, V any] func() (K, V, bool)

MapGenerator produces key-value pairs one at a time. It returns (key, value, true) for each pair and (zeroK, zeroV, false) when the sequence is exhausted.

T type MapIterator

src
type MapIterator[K comparable, V any] interface {
	// MapValues returns an iterator that transforms values by applying f, keeping keys unchanged.
	MapValues(f func(K, V) V) MapIterator[K, V]
	// MapEntries returns an iterator that transforms both keys and values by applying f.
	MapEntries(f func(K, V) (K, V)) MapIterator[K, V]
	// Filter returns an iterator that yields only pairs satisfying pred.
	Filter(pred func(K, V) bool) MapIterator[K, V]
	// Unique returns an iterator that deduplicates pairs by the string key returned by fn.
	Unique(fn func(K, V) string) MapIterator[K, V]
	// MapValuesIndex returns an iterator that transforms values by applying f with a 0-based index.
	MapValuesIndex(f func(int, K, V) V) MapIterator[K, V]
	// MapEntriesIndex returns an iterator that transforms both keys and values by applying f with a 0-based index.
	MapEntriesIndex(f func(int, K, V) (K, V)) MapIterator[K, V]
	// FilterIndex returns an iterator that yields only pairs satisfying pred, which receives a 0-based index.
	FilterIndex(pred func(int, K, V) bool) MapIterator[K, V]
	// Apply exhausts the iterator, calling f for each key-value pair.
	Apply(f func(K, V))
	// Collect collects all remaining pairs into a map.
	Collect() map[K]V
	// Materialize materializes all pairs into a map and returns a new iterator over it.
	// If less is non-nil the keys are visited in sorted order; otherwise the order is unspecified.
	Materialize(less func(K, K) bool) MapIterator[K, V]
	// Skip returns an iterator that discards the first count pairs.
	// Panics if count is negative.
	Skip(count int) MapIterator[K, V]
	// Limit returns an iterator that yields at most count elements.
	// Panics if count is negative.
	Limit(count int) MapIterator[K, V]
	// Keys returns a ListIterator that yields the keys.
	Keys() ListIterator[K]
	// Values returns a ListIterator that yields the values.
	Values() ListIterator[V]
}

MapIterator is a lazy, pull-based iterator over key-value pairs. All intermediate operations (MapValues, MapEntries, Filter, Skip, Limit, etc.) return a new iterator; terminal operations (Apply, Collect) consume it.

f func FromMap

src
func FromMap[K comparable, V any](inp map[K]V, less func(K, K) bool) MapIterator[K, V]

FromMap creates a MapIterator that yields the entries of inp. If less is non-nil the keys are visited in sorted order; otherwise the iteration order is unspecified (uses reflect.MapRange).

f func NewMapIterator

src
func NewMapIterator[K comparable, V any](gen MapGenerator[K, V], maxElements int) MapIterator[K, V]

NewMapIterator creates a MapIterator backed by gen. maxElements is a hint for the expected upper bound of pairs (used for pre-allocation); pass -1 if unknown.

T type Tuple2

src
type Tuple2[T1 any, T2 any] struct {
	// contains filtered or unexported fields
}

Tuple2 is a generic pair of values.

f func NewTuple2

src
func NewTuple2[T1 any, T2 any](first T1, second T2) Tuple2[T1, T2]

NewTuple2 creates a Tuple2 holding first and second.

m func (*Tuple2[T1, T2]) First

src
func (t *Tuple2[T1, T2]) First() T1

First returns the first element of the pair.

m func (*Tuple2[T1, T2]) Second

src
func (t *Tuple2[T1, T2]) Second() T2

Second returns the second element of the pair.

m func (*Tuple2[T1, T2]) Values

src
func (t *Tuple2[T1, T2]) Values() (T1, T2)

Values returns both elements of the pair.

T type Tuple3

src
type Tuple3[T1 any, T2 any, T3 any] struct {
	// contains filtered or unexported fields
}

Tuple3 is a generic triple of values.

f func NewTuple3

src
func NewTuple3[T1 any, T2 any, T3 any](first T1, second T2, third T3) Tuple3[T1, T2, T3]

NewTuple3 creates a Tuple3 holding first, second, and third.

m func (*Tuple3[T1, T2, T3]) First

src
func (t *Tuple3[T1, T2, T3]) First() T1

First returns the first element of the triple.

m func (*Tuple3[T1, T2, T3]) Second

src
func (t *Tuple3[T1, T2, T3]) Second() T2

Second returns the second element of the triple.

m func (*Tuple3[T1, T2, T3]) Third

src
func (t *Tuple3[T1, T2, T3]) Third() T3

Third returns the third element of the triple.

m func (*Tuple3[T1, T2, T3]) Values

src
func (t *Tuple3[T1, T2, T3]) Values() (T1, T2, T3)

Values returns all three elements of the triple.