slices

package
v0.22.5 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const MaxConcurrency = uint16(0)

Variables

This section is empty.

Functions

func Accumulate

func Accumulate[R any, T any, AF AccumulatorFunc[T, R], S ~[]T](ss S, accumulator AF) R

Successively applies the accumulator function to each element of a slice, producing a single accumulated (reduced) result.

func AccumulateIf

func AccumulateIf[R any, T any, SF SelectFunc[T], AF AccumulatorFunc[T, R], S ~[]T](ss S, selector SF, accumulator AF) R

func All

func All[T any, SF SelectFunc[T], S ~[]T](ss S, selector SF) bool

func Any

func Any[T any, SF SelectFunc[T], S ~[]T](ss S, selector SF) bool

func Contains

func Contains[T comparable, S ~[]T](ss S, s T) bool

func Diff

func Diff[T cmp.Ordered, S []T](a, b S) (S, S)

Computes set difference between two slices, returning a slice of elements that are in `a` but not in `b`, and a slice of elements that are in `b` but not in `a`.

func DiffFunc

func DiffFunc[T any, EF EqualFunc[T], S ~[]T](a, b S, equalFunc EF) S

Returns a set difference between two slices, using custom equality function.

func Flatten

func Flatten[T any, S ~[]T, SS ~[]S](ss SS) S

Flatten takes a list of lists and puts all elements into a single list.

func GroupBy

func GroupBy[S ~[]T, K comparable, KF KeyFunc[T, K], T any](ss S, keyFunc KF) map[K]S

Groups elements of a slice by a key, using a custom function to compute the key for each slice element.

func Index

func Index[T comparable, S ~[]T](ss S, s T) int

Returns the index of the first occurrence of `s` in `ss`, or -1 if not found.

func IndexFunc

func IndexFunc[T any, SF SelectFunc[T], S ~[]T](ss S, selector SF) int

IndexFunc returns the index of the first element that matches the selector function, or -1 if no such element is found. The name is not great, but it matches the Go standard library's `slices.IndexFunc` function.

func Intersect

func Intersect[T cmp.Ordered, S []T](a, b S) S

Intersect computes the intersection of two slices of ordered elements. It returns a new slice with elements that exist in both input slices. The resulting slice has no duplicates and elements are in sorted order.

func IntersectFunc

func IntersectFunc[T any, EF EqualFunc[T], S ~[]T](a, b S, equalFunc EF) S

IntersectFunc computes the intersection of two slices by using a provided function to determine if two elements are equal. It returns a new slice with elements that exist in both input slices. The resulting slice has no duplicates and preserves the order from the first slice.

func LenIf

func LenIf[T any, SF SelectFunc[T], S ~[]T](ss S, selector SF) int

func Map

func Map[R any, T any, MF MapFunc[T, R], S ~[]T](ss S, mapping MF) []R

func MapConcurrent

func MapConcurrent[R any, T any, MF MapFunc[T, R], S ~[]T](ss S, mapping MF, concurrency uint16) []R

MapConcurrent will call the mapping functions concurrently, up to specified concurrency level. If concurrency == 0 (MaxConcurrency), every function will be called using separate goroutine.

func NonEmpty

func NonEmpty[E any, T ~string | ~[]E, S ~[]T](ss S) S

func Select

func Select[T any, SF SelectFunc[T], S ~[]T](ss S, selector SF) S

func SeqIndex

func SeqIndex[T comparable, S ~[]T](ss S, seq S) int

Returns the index of the first occurrence of `seq` in `ss`, or -1 if not found. This is "naive" algorithm that works well for short sequences (less than 5 elements), which is our main use case. If a need arises, we could switch this to Knuth-Morris-Pratt algorithm.

func StartsWith

func StartsWith[T comparable, S ~[]T](ss S, prefix S) bool

func Unique

func Unique[T comparable, S ~[]T](s S) S

Unique returns a slice with unique elements from the input slice.

Types

type AccumulatorFunc

type AccumulatorFunc[T any, R any] interface {
	~func(R, T) R | ~func(R, *T) R
}

The purpose of the accumulator function is to take the "accumulated so far" value (first parameter) and apply the "next element" (second parameter) to it, producing a new "accumulated so far" value. The accumulated value type and the element type can be different. It is the "reduce" operation from map-reduce pattern of large-scale data processing.

type EqualFunc

type EqualFunc[T any] interface {
	~func(T, T) bool | ~func(*T, *T) bool
}

type KeyFunc

type KeyFunc[T any, K comparable] interface {
	~func(T) K | ~func(*T) K
}

type MapFunc

type MapFunc[T any, R any] interface {
	~func(int, T) R | ~func(T) R | ~func(int, *T) R | ~func(*T) R
}

type SelectFunc

type SelectFunc[T any] interface {
	~func(int, T) bool | ~func(T) bool | ~func(int, *T) bool | ~func(*T) bool
}

Jump to

Keyboard shortcuts

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