package slicesx
import "tailscale.com/util/slicesx"
Package slicesx contains some helpful generic slice functions.
Index ¶
- func AppendMatching[T any](dst, ps []T, f func(T) bool) []T
- func AppendNonzero[S ~[]T, T comparable](dst, src S) S
- func CutPrefix[E comparable](s, prefix []E) (after []E, found bool)
- func CutSuffix[E comparable](s, suffix []E) (after []E, found bool)
- func EqualSameNil[S ~[]E, E comparable](s1, s2 S) bool
- func Filter[S ~[]T, T any](dst, src S, fn func(T) bool) S
- func FirstEqual[T comparable](s []T, v T) bool
- func HasPrefix[E comparable](s, prefix []E) bool
- func HasSuffix[E comparable](s, suffix []E) bool
- func Interleave[S ~[]T, T any](a, b S) S
- func LastEqual[T comparable](s []T, v T) bool
- func MapKeys[M ~map[K]V, K comparable, V any](m M) []K
- func MapValues[M ~map[K]V, K comparable, V any](m M) []V
- func Partition[S ~[]T, T any](s S, cb func(T) bool) (trues, falses S)
- func Shuffle[S ~[]T, T any](s S)
Functions ¶
func AppendMatching ¶
AppendMatching appends elements in ps to dst if f(x) is true.
func AppendNonzero ¶
func AppendNonzero[S ~[]T, T comparable](dst, src S) S
AppendNonzero appends all non-zero elements of src to dst.
func CutPrefix ¶
func CutPrefix[E comparable](s, prefix []E) (after []E, found bool)
CutPrefix returns s without the provided leading prefix slice and reports whether it found the prefix. If s doesn't start with prefix, CutPrefix returns s, false. If prefix is the empty slice, CutPrefix returns s, true. CutPrefix returns slices of the original slice s, not copies.
func CutSuffix ¶
func CutSuffix[E comparable](s, suffix []E) (after []E, found bool)
CutSuffix returns s without the provided ending suffix slice and reports whether it found the suffix. If s doesn't end with suffix, CutSuffix returns s, false. If suffix is the empty slice, CutSuffix returns s, true. CutSuffix returns slices of the original slice s, not copies.
func EqualSameNil ¶
func EqualSameNil[S ~[]E, E comparable](s1, s2 S) bool
EqualSameNil reports whether two slices are equal: the same length, same nilness (notably when length zero), and all elements equal. If the lengths are different or their nilness differs, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.
It is identical to the standard library's slices.Equal but adds the matching nilness check.
func Filter ¶
Filter calls fn with each element of the provided src slice, and appends the element to dst if fn returns true.
dst can be nil to allocate a new slice, or set to src[:0] to filter in-place without allocating.
func FirstEqual ¶
func FirstEqual[T comparable](s []T, v T) bool
FirstEqual reports whether len(s) > 0 and its first element == v.
func HasPrefix ¶
func HasPrefix[E comparable](s, prefix []E) bool
HasPrefix reports whether the byte slice s begins with prefix.
func HasSuffix ¶
func HasSuffix[E comparable](s, suffix []E) bool
HasSuffix reports whether the slice s ends with suffix.
func Interleave ¶
func Interleave[S ~[]T, T any](a, b S) S
Interleave combines two slices of the form [a, b, c] and [x, y, z] into a slice with elements interleaved; i.e. [a, x, b, y, c, z].
func LastEqual ¶
func LastEqual[T comparable](s []T, v T) bool
LastEqual reports whether len(s) > 0 and its last element == v.
func MapKeys ¶
func MapKeys[M ~map[K]V, K comparable, V any](m M) []K
MapKeys returns the values of the map m.
The keys will be in an indeterminate order.
It's equivalent to golang.org/x/exp/maps.Keys, which unfortunately has the package name "maps", shadowing the std "maps" package. This version exists for clarity when reading call sites.
As opposed to slices.Collect(maps.Keys(m)), this allocates the returned slice once to exactly the right size, rather than appending larger backing arrays as it goes.
func MapValues ¶
func MapValues[M ~map[K]V, K comparable, V any](m M) []V
MapValues returns the values of the map m.
The values will be in an indeterminate order.
It's equivalent to golang.org/x/exp/maps.Values, which unfortunately has the package name "maps", shadowing the std "maps" package. This version exists for clarity when reading call sites.
As opposed to slices.Collect(maps.Values(m)), this allocates the returned slice once to exactly the right size, rather than appending larger backing arrays as it goes.
func Partition ¶
Partition returns two slices, the first containing the elements of the input slice for which the callback evaluates to true, the second containing the rest.
This function does not mutate s.
func Shuffle ¶
func Shuffle[S ~[]T, T any](s S)
Shuffle randomly shuffles a slice in-place, similar to rand.Shuffle.
Source Files ¶
slicesx.go
- Version
- v1.84.0 (latest)
- Published
- May 21, 2025
- Platform
- linux/amd64
- Imports
- 2 packages
- Last checked
- 1 day ago –
Tools for package owners.