Documentation
¶
Overview ¶
Package slices provides helpful functions for slices
Index ¶
- func Chunk[T any](slice []T, size int) [][]T
- func Contains[T comparable](slice []T, value T) bool
- func ContainsCount[T comparable](slice []T, value T) int
- func EqualsIgnoreOrder[ComparableSlice ~[]Type, Type comparable](s1 ComparableSlice, s2 ComparableSlice) bool
- func Flatten[T any](slices [][]T) []T
- func IndexOf[T comparable](slice []T, value T) int
- func IndexOfAll[T comparable](slice []T, value T) []int
- func Reverse[T any](slice []T)
- func Unique[T comparable](slice []T) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶
Chunk returns a slice of slices of size Each of size specified - last may be smaller Panics when size <= 0
func Contains ¶
func Contains[T comparable](slice []T, value T) bool
Contains returns true if value is found Empty slice always returns false
func ContainsCount ¶
func ContainsCount[T comparable](slice []T, value T) int
ContainsCount gives back the count of value findings
func EqualsIgnoreOrder ¶
func EqualsIgnoreOrder[ComparableSlice ~[]Type, Type comparable](s1 ComparableSlice, s2 ComparableSlice) bool
EqualsIgnoreOrder compares two splices ignoring the order of elements It returns true if both slices contain the same elements (and same frequency of same elements), regardless of order
func Flatten ¶ added in v0.2.0
func Flatten[T any](slices [][]T) []T
Flatten takes a slice of slices of type T and returns a single slice containing all elements of the nested slices in the same order.
The function first calculates the total number of elements across all inner slices to allocate the output slice with the correct capacity, improving performance by avoiding multiple reallocations. It then appends each inner slice to the output slice sequentially.
Example:
nums := [][]int{{1, 2}, {3, 4, 5}}
flat := Flatten(nums) // flat == []int{1, 2, 3, 4, 5}
Type Parameters:
- T: any type
func IndexOf ¶
func IndexOf[T comparable](slice []T, value T) int
IndexOf returns index of first match. Similar to strings.Index Empty slice / no finding returns -1
func IndexOfAll ¶
func IndexOfAll[T comparable](slice []T, value T) []int
IndexOfAll returns all positions of value findings
func Unique ¶
func Unique[T comparable](slice []T) []T
Unique returns a new slice containing only the unique values from slice (deduplication)
Types ¶
This section is empty.