Documentation
¶
Overview ¶
Package collection provides focused transformations over ordinary Go slices. Inputs remain unchanged; callbacks run synchronously. Result containers are independent, while pointer/map/slice elements retain ordinary Go aliasing. Use slices/maps/iter for in-place sorting, reversing, cloning and iteration; SortBy/SortByDesc return new slices ordered by an extracted key.
Index ¶
- func All[S ~[]T, T any](input S, match func(T) bool) bool
- func Any[S ~[]T, T any](input S, match func(T) bool) bool
- func Average[S ~[]T, T Number](input S) (float64, bool)
- func AverageBy[S ~[]T, T any, N Number](input S, value func(T) N) (float64, bool)
- func Chunk[S ~[]T, T any](input S, size int) []S
- func Count[S ~[]T, T any](input S, match func(T) bool) int
- func CountBy[S ~[]T, T any, K comparable](input S, key func(T) K) map[K]int
- func Filter[S ~[]T, T any](input S, keep func(T) bool) S
- func Find[S ~[]T, T any](input S, match func(T) bool) (T, bool)
- func FlatMap[S ~[]T, T, U any](input S, transform func(T) []U) []U
- func GroupBy[S ~[]T, T any, K comparable](input S, key func(T) K) map[K][]T
- func KeyBy[S ~[]T, T any, K comparable](input S, key func(T) K) map[K]T
- func Map[S ~[]T, T, U any](input S, transform func(T) U) []U
- func MaxBy[S ~[]T, T any, K cmp.Ordered](input S, key func(T) K) (T, bool)
- func MinBy[S ~[]T, T any, K cmp.Ordered](input S, key func(T) K) (T, bool)
- func Partition[S ~[]T, T any](input S, keep func(T) bool) (S, S)
- func Reduce[S ~[]T, T, U any](input S, initial U, combine func(U, T) U) U
- func SortBy[S ~[]T, T any, K cmp.Ordered](input S, key func(T) K) S
- func SortByDesc[S ~[]T, T any, K cmp.Ordered](input S, key func(T) K) S
- func Sum[S ~[]T, T Number](input S) T
- func SumBy[S ~[]T, T any, N Number](input S, value func(T) N) N
- func Unique[S ~[]T, T comparable](input S) S
- func UniqueBy[S ~[]T, T any, K comparable](input S, key func(T) K) S
- type Number
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Average ¶
Average returns the float64 arithmetic mean, or false for no values. The running total is float64, so integers beyond 2^53 lose precision; use decimal.Sum and Div for exact results.
func Chunk ¶
Chunk splits input into consecutive chunks of at most size items. Chunks share one new backing array, independent of input, and each chunk's capacity equals its length, so appending to one chunk never overwrites the next. Nil input returns nil. A size below one panics, like slices.Chunk.
func CountBy ¶
func CountBy[S ~[]T, T any, K comparable](input S, key func(T) K) map[K]int
CountBy counts items per key. Map iteration has normal unspecified order.
func FlatMap ¶
func FlatMap[S ~[]T, T, U any](input S, transform func(T) []U) []U
FlatMap concatenates each transformed slice in input order, preserving nil input.
func GroupBy ¶
func GroupBy[S ~[]T, T any, K comparable](input S, key func(T) K) map[K][]T
GroupBy preserves input order inside every group. Map iteration is unordered.
func KeyBy ¶
func KeyBy[S ~[]T, T any, K comparable](input S, key func(T) K) map[K]T
KeyBy keeps the last item for each key, matching ordinary map assignment.
func Map ¶
func Map[S ~[]T, T, U any](input S, transform func(T) U) []U
Map transforms each element into a new slice in input order, preserving nil input.
func Partition ¶
Partition splits input into matching and nonmatching slices, preserving order within each. Nil input returns two nil slices.
func Reduce ¶
func Reduce[S ~[]T, T, U any](input S, initial U, combine func(U, T) U) U
Reduce combines elements in input order, starting with the supplied initial value.
func SortBy ¶
SortBy returns a new slice ordered by ascending key. The sort is stable, and key runs exactly once per item. Floating-point NaN keys sort first, following cmp.Compare.
func SortByDesc ¶
SortByDesc is SortBy with descending keys; equal keys keep input order.
func Sum ¶
func Sum[S ~[]T, T Number](input S) T
Sum adds values with ordinary Go arithmetic, including integer wraparound.
func Unique ¶
func Unique[S ~[]T, T comparable](input S) S
Unique retains the first occurrence of each value, in input order.
func UniqueBy ¶
func UniqueBy[S ~[]T, T any, K comparable](input S, key func(T) K) S
UniqueBy retains the first item for each key, in input order.