slices2

package
v0.0.0-...-2aa3dc7 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear[S ~[]T, T any](s S) S

Clear zeros the slice and returns it.

func FilterInPlace

func FilterInPlace[S ~[]T, T any](s S, include func(T) bool) S

FilterInPlace filters the slice in place and returns the filtered slice.

func Insert

func Insert[S ~[]T, T any](s S, index int, item T) []T

Insert inserts an item at an index.

func Pop

func Pop[S ~[]T, T any](sptr *S) T

Pop removes the last item from the slice and returns it. If the slice is empty, it panics. Internally uses slices.Pop to remove the last item.

func Random

func Random[S ~[]T, T any](s S) T

Random returns a random item from the slice, panics if the slice is empty. Internally uses math/rand.Intn to generate a random index.

func Remove

func Remove[S ~[]T, T comparable](s S, item T) S

Remove removes the first occurrence of the item from the slice.

func RemoveAt

func RemoveAt[S ~[]T, T any](s S, index int, n int) S

RemoveAt removes n items at an index, and truncates the slice.

func Replace

func Replace[S ~[]T, T comparable](s S, oldItem, newItem T) S

Replace replaces the first occurrence of oldItem with newItem in the slice. If oldItem is not found, the slice is returned unchanged.

func ShiftLeft

func ShiftLeft[S ~[]T, T any](s S, n int) S

ShiftLeft shifts the slice left by n, and returns the truncated slice.

func Shuffle

func Shuffle[S ~[]T, T any](s S)

Shuffle pseudo-randomizes the order of elements using rand.Shuffle.

func SortLess

func SortLess[S ~[]T, T any](s S, less func(a, b T) bool)

SortLess sorts a slice.

func Truncate

func Truncate[S ~[]T, T any](s S) S

Truncate clears and truncates the slice, returns nil if the slice is nil.

Usage:

s := []int{1, 2, 3}
s = slices2.Truncate(s)

Types

This section is empty.

Jump to

Keyboard shortcuts

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