slices

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 1 Imported by: 7

Documentation

Overview

Package slices provides utilities for safe slice operations with generics

Index

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 Filter

func Filter[S ~[]E, E any](s S, predicate func(E) bool) S

Filter creates a new slice with elements that pass the test

func FindIndex

func FindIndex[S ~[]E, E any](s S, predicate func(E) bool) int

FindIndex returns the index of the first element satisfying the predicate, or -1 if no such element is found.

func First

func First[S ~[]E, E any](s S) (E, bool)

First returns the first element if slice is not empty

func Flatten

func Flatten[S ~[]E, E any](ss []S) S

Flatten concatenates a slice of slices into a single slice.

func Get

func Get[S ~[]E, E any](s S, idx int) (E, bool)

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

func GetAny[O any](vals []any, idx int) (O, bool)

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

func GetPtr[S ~[]E, E any](s S, idx int) *E

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

func GetWithDefault[S ~[]E, E any](s S, idx int, defaultVal E) E

GetWithDefault retrieves an element or returns a default value

func IsEmpty

func IsEmpty[S ~[]E, E any](s S) bool

IsEmpty checks if slice is nil or empty

func IsValidIndex

func IsValidIndex[S ~[]E, E any](s S, idx int) bool

IsValidIndex checks if index is valid for the slice.

func Last

func Last[S ~[]E, E any](s S) (E, bool)

Last returns the last element if slice is not empty

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

func Slice[S ~[]E, E any](s S, start int) S

Slice returns a sub-slice with bounds checking Automatically adjusts start and end to valid ranges

func TryGet

func TryGet[S ~[]E, E any](s S, idx int) E

TryGet retrieves an element or returns zero value if index is out of bounds

func TryGetAny

func TryGetAny[O any](vals []any, idx int) O

TryGetAny retrieves and type-asserts an element from []any or returns zero value

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.

Jump to

Keyboard shortcuts

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