collection

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[S ~[]T, T any](input S, match func(T) bool) bool

func Any

func Any[S ~[]T, T any](input S, match func(T) bool) bool

func Average

func Average[S ~[]T, T Number](input S) (float64, bool)

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 AverageBy

func AverageBy[S ~[]T, T any, N Number](input S, value func(T) N) (float64, bool)

AverageBy is Average over one selected number per item.

func Chunk

func Chunk[S ~[]T, T any](input S, size int) []S

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 Count

func Count[S ~[]T, T any](input S, match func(T) bool) int

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 Filter

func Filter[S ~[]T, T any](input S, keep func(T) bool) S

func Find

func Find[S ~[]T, T any](input S, match func(T) bool) (T, bool)

func FlatMap

func FlatMap[S ~[]T, T, U any](input S, transform func(T) []U) []U

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

func MaxBy

func MaxBy[S ~[]T, T any, K cmp.Ordered](input S, key func(T) K) (T, bool)

MaxBy returns the first item with the largest key, or false for no items.

func MinBy

func MinBy[S ~[]T, T any, K cmp.Ordered](input S, key func(T) K) (T, bool)

MinBy returns the first item with the smallest key, or false for no items.

func Partition

func Partition[S ~[]T, T any](input S, keep func(T) bool) (S, S)

func Reduce

func Reduce[S ~[]T, T, U any](input S, initial U, combine func(U, T) U) U

func SortBy

func SortBy[S ~[]T, T any, K cmp.Ordered](input S, key func(T) K) S

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

func SortByDesc[S ~[]T, T any, K cmp.Ordered](input S, key func(T) K) S

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 SumBy

func SumBy[S ~[]T, T any, N Number](input S, value func(T) N) N

SumBy adds one selected number per item with ordinary Go arithmetic.

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.

Types

type Number

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

Number is the set of built-in numeric types accepted by Sum and Average. Exact decimal and money totals belong to the decimal packages.

Jump to

Keyboard shortcuts

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