Documentation
¶
Overview ¶
Package slices provides utilities for safe slice operations with generics
Index ¶
- func Contains[S ~[]E, E comparable](s S, val E) bool
- func Filter[S ~[]E, E any](s S, predicate func(E) bool) S
- func FindIndex[S ~[]E, E any](s S, predicate func(E) bool) int
- func First[S ~[]E, E any](s S) (E, bool)
- func Flatten[S ~[]E, E any](ss []S) S
- func Get[S ~[]E, E any](s S, idx int) (E, bool)
- func GetAny[O any](vals []any, idx int) (O, bool)
- func GetPtr[S ~[]E, E any](s S, idx int) *E
- func GetWithDefault[S ~[]E, E any](s S, idx int, defaultVal E) E
- func IsEmpty[S ~[]E, E any](s S) bool
- func IsValidIndex[S ~[]E, E any](s S, idx int) bool
- func Last[S ~[]E, E any](s S) (E, bool)
- func Map[T, R any](vals []T, transform func(T) R) []R
- func Reduce[T, R any](vals []T, initial R, reducer func(R, T) R) R
- func Slice[S ~[]E, E any](s S, start int) S
- func TryGet[S ~[]E, E any](s S, idx int) E
- func TryGetAny[O any](vals []any, idx int) O
- func Unique[S ~[]E, E comparable](s S) S
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[S ~[]E, E comparable](s S, val E) bool
Contains reports whether the slice contains the given value.
func FindIndex ¶
FindIndex returns the index of the first element satisfying the predicate, or -1 if no such element is found.
func Flatten ¶
func Flatten[S ~[]E, E any](ss []S) S
Flatten concatenates a slice of slices into a single slice.
func Get ¶
Get safely retrieves an element from a slice with bounds checking Returns the element and true if successful, zero value and false otherwise
func GetAny ¶
GetAny safely retrieves and type-asserts an element from []any slice Returns the converted element and true if successful, zero value and false otherwise
func GetPtr ¶
GetPtr returns a pointer to the element if it exists, nil otherwise Useful when you need to distinguish between zero value and missing element
func GetWithDefault ¶
GetWithDefault retrieves an element or returns a default value
func IsValidIndex ¶
IsValidIndex checks if index is valid for the slice.
func Map ¶
func Map[T, R any](vals []T, transform func(T) R) []R
Map transforms each element and returns a new slice
func Reduce ¶
func Reduce[T, R any](vals []T, initial R, reducer func(R, T) R) R
Reduce applies a function against elements to reduce to a single value
func Slice ¶
Slice returns a sub-slice with bounds checking Automatically adjusts start and end to valid ranges
func Unique ¶
func Unique[S ~[]E, E comparable](s S) S
Unique returns a new slice with duplicate elements removed, preserving order.
Types ¶
This section is empty.