slicesx

package
v1.78.2 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: BSD-3-Clause Imports: 2 Imported by: 9

Documentation

Overview

Package slicesx contains some helpful generic slice functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendMatching added in v1.66.0

func AppendMatching[T any](dst, ps []T, f func(T) bool) []T

AppendMatching appends elements in ps to dst if f(x) is true.

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

func Filter[S ~[]T, T any](dst, src S, fn func(T) bool) S

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 Partition added in v1.44.0

func Partition[S ~[]T, T any](s S, cb func(T) bool) (trues, falses S)

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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