Documentation ¶
Overview ¶
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)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendMatching ¶ added in v1.66.0
AppendMatching appends elements in ps to dst if f(x) is true.
func AppendNonzero ¶ added in v1.80.0
func AppendNonzero[S ~[]T, T comparable](dst, src S) S
AppendNonzero appends all non-zero elements of src to dst.
func CutPrefix ¶ added in v1.72.0
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 ¶ added in v1.72.0
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 ¶ added in v1.52.0
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 ¶ added in v1.60.0
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 ¶ added in v1.74.0
func FirstEqual[T comparable](s []T, v T) bool
FirstEqual reports whether len(s) > 0 and its first element == v.
func HasPrefix ¶ added in v1.72.0
func HasPrefix[E comparable](s, prefix []E) bool
HasPrefix reports whether the byte slice s begins with prefix.
func HasSuffix ¶ added in v1.72.0
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 ¶ added in v1.74.0
func LastEqual[T comparable](s []T, v T) bool
LastEqual reports whether len(s) > 0 and its last element == v.
func MapKeys ¶ added in v1.80.0
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 ¶ added in v1.80.0
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.
Types ¶
This section is empty.