types

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Index

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 GetValue added in v0.3.0

func GetValue[T any](ptr *T, defaultVal T) T

func GetValueOrDefault added in v0.6.0

func GetValueOrDefault[T any](items []T, index int, defaultValue T) T

func MapSlice added in v0.6.0

func MapSlice[S any, T any](items []S, mapper func(S) T) []T

func MapSlicePtr added in v0.6.0

func MapSlicePtr[S any, T any](items *[]S, mapper func(S) T) []T

func NewStringSet

func NewStringSet() *stringSet

func ToPtr

func ToPtr[T any](value T) *T

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

func UniqueFunc[T any](items []T, cmp func(T, T) bool) []T

Types

This section is empty.

Jump to

Keyboard shortcuts

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