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 and 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 official Go documentation for the `slices` package.
Example (Sorting and Searching):
numbers := []int{3, 1, 4, 1, 5, 9}
// Check if a slice contains a value
_ = slices.Contains(numbers, 5) // true
// Sort the slice in place
slices.Sort(numbers)
// numbers is now [1, 1, 3, 4, 5, 9]
// Find the index of a value in a sorted slice
idx, found := slices.BinarySearch(numbers, 4)
// idx is 3, found is true
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 ¶
- Variables
- func Append[T ~[]S, S any](arr T, v S) (T, int)
- func BinarySearch[S ~[]E, E constraints.Ordered](x S, target E) (int, bool)
- func BinarySearchFunc[S ~[]E, E, T any](x S, target T, cmp func(E, T) int) (int, bool)
- func Clip[S ~[]E, E any](s S) S
- func Clone[S ~[]E, E any](s S) S
- func Compact[S ~[]E, E comparable](s S) S
- func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S
- func Compare[S ~[]E, E constraints.Ordered](s1, s2 S) int
- func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int
- func Contains[S ~[]E, E comparable](s S, v E) bool
- func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool
- func CopyAt[T ~[]S, S any](s, t T, i int) T
- func Count[T ~[]S, S E](s, sub T) int
- func CountArray[T ~[]S, S E](ss T, s S) int
- func Cut[T ~[]S, S E](s, sep T) (before, after T, found bool)
- func Delete[S ~[]E, E any](s S, i, j int) S
- func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S
- func Equal[S ~[]E, E comparable](s1, s2 S) bool
- func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool
- func Filter[T ~[]S, S any](s T, f func(S) bool) T
- func Grow[S ~[]E, E any](s S, n int) S
- func Index[S ~[]E, E comparable](s S, v E) int
- func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int
- func IndexSlice[T ~[]S, S E](s, substr T) int
- func Insert[S ~[]E, E any](s S, i int, v ...E) S
- func InsertWith[T ~[]S, S any](s T, v S, fn func(a, b S) bool) T
- func IsSorted[S ~[]E, E constraints.Ordered](x S) bool
- func IsSortedFunc[S ~[]E, E any](x S, cmp func(a, b E) int) bool
- func Join[T ~[]S, S any](s []T, sep T) T
- func LastIndexSlice[T ~[]S, S E](s, sep T) int
- func Map[S, T any](s []S, f func(S) T) []T
- func Max[S ~[]E, E constraints.Ordered](x S) E
- func MaxFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E
- func Min[S ~[]E, E constraints.Ordered](x S) E
- func MinFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E
- func OverWithError[S any](s []S, err error) func(func(int, S) bool)
- func Read[T ~[]S, S any](arr T, offset int, limit int) T
- func Reduce[S, T any](s []S, initial T, f func(T, S) T) T
- func RemoveWith[T ~[]S, S any](s T, fn func(a S) bool) T
- func Repeat[T ~[]S, S any](b T, count int) T
- func Replace[S ~[]E, E any](s S, i, j int, v ...E) S
- func Reverse[S ~[]E, E any](s S)
- func Sort[S ~[]E, E constraints.Ordered](x S)
- func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int)
- func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int)
- func Split[T ~[]S, S E](s, sep T) []T
- func Transform[TS ~[]S, S any, T any](s TS, f func(S) (T, bool)) []T
- func Unique[T ~[]S, S E](s T) T
- type E
Constants ¶
This section is empty.
Variables ¶
var ( // ErrWrongIndex is an error when index is out of range ErrWrongIndex = errors.New("slices.Array: wrong index") )
Functions ¶
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 Compact ¶ added in v0.4.0
func Compact[S ~[]E, E comparable](s S) S
func CompactFunc ¶ added in v0.4.0
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 Contains ¶
func Contains[S ~[]E, E comparable](s S, v E) bool
func ContainsFunc ¶ added in v0.4.0
func CopyAt ¶
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 ¶
Count counts the number of non-overlapping instances of substr in s. If substr is an empty Array, Count returns 1 + the number of Unicode code points in s.
func CountArray ¶
CountArray counts the number of non-overlapping instances of c in s.
func Cut ¶
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 DeleteFunc ¶ added in v0.4.0
func Equal ¶
func Equal[S ~[]E, E comparable](s1, s2 S) bool
func Filter ¶ added in v0.4.0
Filter returns a new slice containing all elements of s for which f(s) is true.
func Index ¶
func Index[S ~[]E, E comparable](s S, v E) int
func IndexSlice ¶ added in v0.4.0
IndexSlice returns the index of the first instance of substr in s, or -1 if substr is not present in s.
func InsertWith ¶
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 Join ¶
func Join[T ~[]S, S any](s []T, sep T) T
Join concatenates the elements of its first argument to create a single Array[S]. The separator Array[S] sep is placed between elements in the resulting Array[S].
func LastIndexSlice ¶ added in v0.4.0
LastIndexSlice returns the index of the last instance of substr in s, or -1 if substr is not present in s.
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 by applying a function to each element.
func Max ¶ added in v0.4.0
func Max[S ~[]E, E constraints.Ordered](x S) E
func Min ¶ added in v0.4.0
func Min[S ~[]E, E constraints.Ordered](x S) E
func OverWithError ¶
OverWithError returns an iterator function for a slice that may have an associated error. The returned iterator will only yield values if the provided error is nil and the slice is not empty. This is useful for chaining operations that can fail.
func Read ¶
Read returns a slice of the Array[S] s beginning at offset and length limit. If offset or limit is negative, it is treated as if it were zero.
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. It iterates through the slice, applying the function 'f' to an accumulator and the current element.
func RemoveWith ¶
RemoveWith removes the first index where fn(a, b) is true.
func Repeat ¶
Repeat returns a new Array[S] consisting of count copies of the Array[S] s.
It panics if count is negative or if the result of (len(s) * count) overflows.
func Sort ¶ added in v0.4.0
func Sort[S ~[]E, E constraints.Ordered](x S)
func SortStableFunc ¶ added in v0.4.0
func Split ¶ added in v0.4.0
func Split[T ~[]S, S E](s, sep T) []T
Split slices s into all subslices separated by sep and returns a slice of the subslices between those separators.
Types ¶
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`). |