slice

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 3 Imported by: 17

README

slice

Generic free functions over plain []T — membership (Contains, ContainsAny, ContainsAll), bounds-safe access (At, AtOK), set operations (Difference, Equal), transforms (Map, Filter, Find, First, NonZero), grouping (Grouper), and Shuffle. Part of rosetta.

These operate on raw slices and return new slices; contrast with sliceof, whose types attach methods to named slice types for schema integration.

What matters here

  • At / AtOK are bounds-safe — they never panic. A negative or out-of-range index returns the element type's zero value (At) or (zero, false) (AtOK). Use these instead of raw slice[i] when the index comes from untrusted or computed input.
  • Shuffle uses math/rand, intentionally — ordering is not security-sensitive. Do not switch it to crypto/rand.
  • Transform functions return new slices and do not mutate the input (Filter, Map, NonZero, Difference). The input slice's backing array is left alone, so callers can keep using it.
  • Grouper requires elements that expose a string field via the stringOKGetter interface. It groups a slice by a named field for rendering; the element type constraint is interface-based, not comparable.

Documentation

Overview

Package slice provides generic utility functions for working with slices: membership tests (Contains, ContainsAll, ContainsAny), transformations (Map, Filter, Unique, Reverse, Shuffle, Difference, NonZero), and safe element access (At, AtOK, Find, First, Split, RemoveAt).

At and its siblings are the reason most of this exists: they read an out-of-range index as the zero value rather than panicking, which lets calling code skip the bounds check that would otherwise wrap every access.

These are functions over ordinary []T values. The sliceof package provides named slice types that carry methods and integrate with schema.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func At added in v0.21.5

func At[T any](slice []T, index int) T

At returns a bounds-safe value from a slice. If the index is out of bounds for the slice, then `At` returns the zero value for that type.

func AtOK added in v0.22.0

func AtOK[T any](slice []T, index int) (T, bool)

AtOK returns a bounds-safe value from a slice. If the index is out of bounds for the slice, then `At` returns the zero value for that type.

func Contains added in v0.2.0

func Contains[T comparable](slice []T, value T) bool

Contains scans a slice for a matching value, and returns TRUE if the value is found.

func ContainsAll added in v0.2.0

func ContainsAll[T comparable](slice []T, values ...T) bool

ContainsAll returns TRUE if the slice contains ALL of the provided values

func ContainsAny added in v0.14.0

func ContainsAny[T comparable](slice []T, values ...T) bool

ContainsAny returns TRUE if the slice contains ANY of the provided values

func Difference added in v0.25.3

func Difference[T comparable](a []T, b []T) []T

Difference returns a new slice of the elements in a that are not present in b.

func Equal added in v0.5.1

func Equal[T comparable](value1 []T, value2 []T) bool

Equal returns true if the two slices are identical, having the same items in the same order, with no alterations.

func Filter

func Filter[T any](original []T, keep func(T) bool) []T

Filter returns a new slice containing only the elements for which keep returns TRUE.

func Find added in v0.23.0

func Find[T any](slice []T, f func(T) bool) (T, bool)

Find returns the first element in the slice that satisfies the provided function.

func First added in v0.11.0

func First[T any](original []T, keep func(T) bool) T

First returns the first element for which keep returns TRUE, or the zero value if none match.

func Map

func Map[T1 any, T2 any](source []T1, delta func(T1) T2) []T2

Map returns a new slice containing the result of applying delta to each element of source.

func NonZero added in v0.19.0

func NonZero[T comparable](original []T) []T

NonZero filters out all zero values from a slice

func NotContains added in v0.25.9

func NotContains[T comparable](slice []T, value T) bool

NotContains returns TRUE if the value DOES NOT exist in the slice

func NotEqual added in v0.24.8

func NotEqual[T comparable](value1 []T, value2 []T) bool

NotEqual returns TRUE if the two slices are NOT identical

func Range added in v0.25.11

func Range[V any](value []V) iter.Seq2[int, V]

Range returns an iterator that yields each value in a slice.

func RemoveAt added in v0.25.5

func RemoveAt[T comparable](slice []T, index int) []T

RemoveAt returns the slice with the element at index removed; an out-of-range index is a no-op.

func Reverse added in v0.19.0

func Reverse[T any](x []T) []T

Reverse reverses the order of the elements in the slice in place, and returns it.

func Shuffle added in v0.25.8

func Shuffle[T any](x []T) []T

Shuffle randomizes the order of the elements in the slice in place, and returns it.

func Split added in v0.23.1

func Split[T any](slice []T) (T, []T)

Split returns the first element of the slice and a slice of the remaining elements. An empty slice yields the zero value and the original (empty) slice.

func Unique added in v0.19.0

func Unique[T comparable](original []T) []T

Unique returns a new slice with all duplicate values removed.

Types

type Grouper added in v0.25.20

type Grouper[T stringOKGetter] struct {
	// contains filtered or unexported fields
}

Grouper reports group boundaries within a slice that is already sorted by a named string field.

func NewGrouper added in v0.25.20

func NewGrouper[T stringOKGetter](slice []T, field string) Grouper[T]

NewGrouper returns a Grouper that detects boundaries in the slice using the named field.

func (Grouper[T]) IsFooter added in v0.25.20

func (grouper Grouper[T]) IsFooter(index int) bool

IsFooter returns TRUE if the element at index ends a group (its field differs from the next element).

func (Grouper[T]) IsHeader added in v0.25.20

func (grouper Grouper[T]) IsHeader(index int) bool

IsHeader returns TRUE if the element at index begins a new group (its field differs from the previous element).

Jump to

Keyboard shortcuts

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