Documentation
¶
Overview ¶
Package array provides utilities for manipulating and inspecting slices, with a focus on data processing and transformation.
Index ¶
- func AreIdentical[T comparable](x, y []T) bool
- func Concat[T any](slices ...[]T) []T
- func Contains[T comparable](s []T, e T) bool
- func DistinctCount[T comparable](s []T) map[T]int
- func Every[T any](s []T, test func(T) bool) bool
- func Filter[T any](s []T, keep func(T) bool) []T
- func Find[T any](s []T, match func(T) bool) (T, bool)
- func GroupBy[T any, K comparable](s []T, keyFunc func(T) K) map[K][]T
- func IndexOf[T comparable](s []T, value T) int
- func Join[T any](s []T, sep string) string
- func JoinString[T fmt.Stringer](s []T, sep string) string
- func Map[T, U any](s []T, transform func(T) U) []U
- func MinMax[T constraints.Ordered](s []T) (min T, max T, err error)
- func Partition[T any](s []T, test func(T) bool) (pass, fail []T)
- func Reduce[T, U any](s []T, reducer func(U, T) U, initial U) U
- func RemoveByIndex[T any](s []T, index int) ([]T, error)
- func RemoveByValue[T comparable](s []T, value T) []T
- func Reverse[T any](s []T) []T
- func Shuffle[T any](s []T) []T
- func Some[T any](s []T, test func(T) bool) bool
- func Sort[T any](s []T, less func(a, b T) bool) []T
- func Split[T any](s []T, size int) ([][]T, error)
- func Take[T any](s []T, n int) ([]T, error)
- func Uniq[T comparable](s []T) []T
- type Pair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AreIdentical ¶
func AreIdentical[T comparable](x, y []T) bool
AreIdentical checks if two slices contain the same elements with identical counts. Works with any comparable type.
func Concat ¶
func Concat[T any](slices ...[]T) []T
Concat combines multiple slices into a single slice. Works with any type.
func Contains ¶
func Contains[T comparable](s []T, e T) bool
Contains checks if an element is present in a slice. Works with any comparable type (int, string, etc.).
func DistinctCount ¶
func DistinctCount[T comparable](s []T) map[T]int
DistinctCount returns a map of unique elements to their frequencies. Works with any comparable type.
func Find ¶
Find returns the first element in a slice that satisfies the predicate. Returns the element and a boolean indicating if it was found.
func GroupBy ¶
func GroupBy[T any, K comparable](s []T, keyFunc func(T) K) map[K][]T
GroupBy groups elements by a key function, returning a map of key to slice. The key type K must be comparable; T can be any type.
func IndexOf ¶
func IndexOf[T comparable](s []T, value T) int
IndexOf returns the first index of a value in a slice, or -1 if not found. Works with any comparable type.
func Join ¶
Join concatenates slice elements into a string with a separator. Works with any type that can be formatted with %v.
func JoinString ¶
JoinString concatenates elements using their String() method. Requires T to implement fmt.Stringer.
func Map ¶
func Map[T, U any](s []T, transform func(T) U) []U
Map applies a transformation function to each element, returning a new slice.
func MinMax ¶
func MinMax[T constraints.Ordered](s []T) (min T, max T, err error)
MinMax finds the minimum and maximum values in a slice. Works with ordered types (int, float64, string, etc.).
func Partition ¶
Partition splits a slice into two based on a predicate. Returns elements that pass and fail the test, respectively.
func Reduce ¶
func Reduce[T, U any](s []T, reducer func(U, T) U, initial U) U
Reduce applies a reduction function over the slice, accumulating a result. Starts with an initial value; T and U can be any types.
func RemoveByIndex ¶
RemoveByIndex removes an element at the specified index from a slice. Returns the new slice and an error if index is out of bounds. Does not modify the original slice.
func RemoveByValue ¶
func RemoveByValue[T comparable](s []T, value T) []T
RemoveByValue removes the first occurrence of a value from a slice. Returns the new slice or the original if value not found. Does not modify the original slice.
func Reverse ¶
func Reverse[T any](s []T) []T
Reverse returns a new slice with elements in reversed order.
func Shuffle ¶
func Shuffle[T any](s []T) []T
Shuffle randomly reorders a slice. Uses math/rand/v2 for randomization; does not modify the original slice.
func Sort ¶
Sort sorts the slice using the provided less function. For ordered types, use: Sort(s, func(a, b T) bool { return a < b })
func Split ¶
Split divides a slice into chunks of the specified size. The last chunk may be smaller if the length is not evenly divisible.
func Take ¶
Take returns the first n elements of a slice. Returns all if n >= len(s), or an empty slice with error if n < 0.
func Uniq ¶
func Uniq[T comparable](s []T) []T
Uniq removes duplicates from a slice, preserving order. Works with any comparable type.