Documentation
¶
Index ¶
- func CompareUnorderedSlices[T comparable](a, b []T) bool
- func CompareUnorderedSlicesKey[T any, K comparable](a, b []T, key func(T) K) bool
- func FilterSlice[T comparable](slice []T, filters ...func(value T, index int) (bool, error)) ([]T, error)
- func GetValue[T any](ptr *T, defaultVal T) T
- func GetValueOrDefault[T any](items []T, index int, defaultValue T) T
- func MapSlice[S any, T any](items []S, mapper func(S) T) []T
- func MapSlicePtr[S any, T any](items *[]S, mapper func(S) T) []T
- func NewStringSet() *stringSet
- func ToPtr[T any](value T) *T
- func Unique[T comparable](items []T) []T
- func UniqueComparable[T any, C comparable](items []T, cmp func(T) C) []T
- func UniqueFunc[T any](items []T, cmp func(T, T) bool) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompareUnorderedSlices ¶ added in v0.6.0
func CompareUnorderedSlices[T comparable](a, b []T) bool
CompareUnorderedSlices compares two slices as unordered multisets (i.e., checks if they contain the same elements with the same frequencies, ignoring order). It requires elements of type T to be comparable (e.g., ints, strings).
Parameters:
- a, b: The slices to compare.
Returns true if the slices contain the same elements with the same multiplicities, false otherwise.
Example usage:
a := []int{1, 2, 3}
b := []int{3, 1, 2}
result := CompareUnorderedSlices(a, b) // true
Note: This function has average O(n) time complexity due to the use of a hash map for counting frequencies.
func CompareUnorderedSlicesKey ¶ added in v0.6.0
func CompareUnorderedSlicesKey[T any, K comparable](a, b []T, key func(T) K) bool
CompareUnorderedSlicesKey compares two slices as unordered multisets (i.e., checks if they contain the same elements with the same frequencies, ignoring order). It uses a provided key function to determine equivalence between elements.
Parameters:
- a, b: The slices to compare.
- key: A function that maps each element of type T to a comparable key of type K. Two elements are considered equivalent if key(x) == key(y). This allows custom equality, such as case-insensitive comparison for strings (e.g., key = strings.ToLower).
Returns true if the slices are equivalent under the key function, false otherwise.
Example usage for case-insensitive string comparison:
a := []string{"Apple", "banana"}
b := []string{"apple", "Banana"}
result := CompareUnorderedSlicesKey(a, b, strings.ToLower) // true
Note: This function has average O(n) time complexity assuming good key distribution for hashing in the map.
func FilterSlice ¶
func FilterSlice[T comparable](slice []T, filters ...func(value T, index int) (bool, error)) ([]T, error)
func GetValueOrDefault ¶ added in v0.6.0
func MapSlicePtr ¶ added in v0.6.0
func NewStringSet ¶
func NewStringSet() *stringSet
func Unique ¶ added in v0.6.0
func Unique[T comparable](items []T) []T
func UniqueComparable ¶ added in v0.6.0
func UniqueComparable[T any, C comparable](items []T, cmp func(T) C) []T
func UniqueFunc ¶ added in v0.6.0
Types ¶
This section is empty.