slices

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package slices provides a rich set of generic functions for common operations on slices of any element type.

This package is a generated adapter that mirrors the public API of the standard Go experimental package `golang.org/x/exp/slices`. It offers a convenient way to access these common utilities for searching, sorting, comparing, and manipulating slices.

For detailed information on the behavior of specific functions, please refer to the pkg.go.dev documentation for the `slices` package.

Package slices implements the functions, types, and interfaces for the module.

Package slices contains generated code by adptool.

Package slices implements the functions, types, and interfaces for the module.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWrongIndex is an error returned when an index is out of range.
	ErrWrongIndex = errors.New("slices: index out of range")
)

Functions

func Append

func Append[S any](arr []S, v S) ([]S, int)

Append appends the element v to the end of the slice s.

func BinarySearch added in v0.4.0

func BinarySearch[S ~[]E, E constraints.Ordered](x S, target E) (int, bool)

func BinarySearchFunc added in v0.4.0

func BinarySearchFunc[S ~[]E, E, T any](x S, target T, cmp func(E, T) int) (int, bool)

func Clip added in v0.4.0

func Clip[S ~[]E, E any](s S) S

func Clone added in v0.4.0

func Clone[S ~[]E, E any](s S) S

func Compact added in v0.4.0

func Compact[S ~[]E, E comparable](s S) S

func CompactFunc added in v0.4.0

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

func Compare added in v0.4.0

func Compare[S ~[]E, E constraints.Ordered](s1, s2 S) int

func CompareFunc added in v0.4.0

func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int

func Contains

func Contains[S ~[]E, E comparable](s S, v E) bool

func ContainsFunc added in v0.4.0

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

func CopyAt

func CopyAt[S any](s, t []S, i int) []S

CopyAt copies the elements from t into s at the specified index. It panics if the index is negative. If the required length is greater than the length of s, s is grown to accommodate the new elements.

func Count

func Count[S E](s, sub []S) int

Count counts the number of non-overlapping instances of substr in s.

func CountArray

func CountArray[S E](ss []S, s S) int

CountArray counts the number of occurrences of c in s.

func Cut

func Cut[S E](s, sep []S) (before, after []S, found bool)

Cut slices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, nil, false.

func Delete added in v0.4.0

func Delete[S ~[]E, E any](s S, i, j int) S

func DeleteFunc added in v0.4.0

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

func Equal

func Equal[S ~[]E, E comparable](s1, s2 S) bool

func EqualFunc added in v0.4.0

func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool

func Filter added in v0.4.0

func Filter[S any](s []S, f func(S) bool) []S

Filter returns a new slice containing all elements of s for which f(s) is true.

func FilterExcluded added in v0.6.0

func FilterExcluded[S comparable](s []S, excludes []S) []S

FilterExcluded returns a new slice containing all elements of s that are not present in excludes.

func FilterIncluded added in v0.6.0

func FilterIncluded[S comparable](s []S, includes []S) []S

FilterIncluded returns a new slice containing all elements of s that are present in includes.

func Grow added in v0.4.0

func Grow[S ~[]E, E any](s S, n int) S

func Index

func Index[S ~[]E, E comparable](s S, v E) int

func IndexFunc added in v0.4.0

func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int

func IndexSlice added in v0.4.0

func IndexSlice[S E](s, substr []S) int

IndexSlice returns the index of the first instance of substr in s, or -1 if not present.

func Insert added in v0.4.0

func Insert[S ~[]E, E any](s S, i int, v ...E) S

func InsertWith

func InsertWith[S any](s []S, v S, fn func(a, b S) bool) []S

InsertWith inserts v into s at the first index where fn(a, b) is true.

func IsSorted added in v0.4.0

func IsSorted[S ~[]E, E constraints.Ordered](x S) bool

func IsSortedFunc added in v0.4.0

func IsSortedFunc[S ~[]E, E any](x S, cmp func(a, b E) int) bool

func Join

func Join[S any](s [][]S, sep []S) []S

Join concatenates the elements of s to create a single slice. The separator sep is placed between elements in the resulting slice.

func LastIndexSlice added in v0.4.0

func LastIndexSlice[S E](s, sep []S) int

LastIndexSlice returns the index of the last instance of sep in s, or -1 if not present.

func Map added in v0.4.0

func Map[S, T any](s []S, f func(S) T) []T

Map transforms a slice of one type to a slice of another type using a mapping function.

func Max added in v0.4.0

func Max[S ~[]E, E constraints.Ordered](x S) E

func MaxFunc added in v0.4.0

func MaxFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E

func Min added in v0.4.0

func Min[S ~[]E, E constraints.Ordered](x S) E

func MinFunc added in v0.4.0

func MinFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E

func OverWithError

func OverWithError[S any](s []S, err error) func(func(int, S) bool)

OverWithError returns an iterator function for a slice that may have an associated error.

func Read

func Read[S any](arr []S, offset int, limit int) []S

Read returns a slice of s beginning at offset and with a length of limit.

func Reduce added in v0.4.0

func Reduce[S, T any](s []S, initial T, f func(T, S) T) T

Reduce aggregates all elements of a slice into a single value by applying a function.

func RemoveWith

func RemoveWith[S any](s []S, fn func(a S) bool) []S

RemoveWith removes elements from a slice based on a predicate function.

func Repeat

func Repeat[S any](b []S, count int) []S

Repeat returns a new slice consisting of count copies of the slice s.

func Replace added in v0.4.0

func Replace[S ~[]E, E any](s S, i, j int, v ...E) S

func Reverse added in v0.4.0

func Reverse[S ~[]E, E any](s S)

func Sort added in v0.4.0

func Sort[S ~[]E, E constraints.Ordered](x S)

func SortFunc added in v0.4.0

func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int)

func SortStableFunc added in v0.4.0

func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int)

func Split added in v0.4.0

func Split[S E](s, sep []S) [][]S

Split slices s into all subslices separated by sep.

func Transform added in v0.2.5

func Transform[S any, T any](s []S, f func(S) (T, bool)) []T

Transform combines mapping and filtering a slice.

func Unique added in v0.4.0

func Unique[S E](s []S) []S

Unique returns a new slice with duplicate elements removed.

Types

type E

type E = comparable

E is a shorthand for the comparable constraint.

Directories

Path Synopsis
Package bytes contains generated code by adptool.
Package bytes contains generated code by adptool.
Package runes provides a rich set of functions for the manipulation of rune slices (`[]rune`).
Package runes provides a rich set of functions for the manipulation of rune slices (`[]rune`).

Jump to

Keyboard shortcuts

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