slices

package
v0.0.0-...-9d022b6 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package slices defines various functions useful with slices of any type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[Slice ~[]E, E any](s Slice) iter.Seq2[int, E]

All returns an iterator over index-value pairs in the slice in the usual order.

func AppendSeq

func AppendSeq[Slice ~[]E, E any](s Slice, seq iter.Seq[E]) Slice

AppendSeq appends the values from seq to the slice and returns the extended slice.

func AppendSeq2

func AppendSeq2[KSlice ~[]K, VSlice ~[]V, K, V any](kSlice KSlice, vSlice VSlice, seq iter.Seq2[K, V]) (KSlice, VSlice)

AppendSeq2 appends the elements of a sequence of pairs to two slices, one for keys and one for values.

func Backward

func Backward[Slice ~[]E, E any](s Slice) iter.Seq2[int, E]

Backward returns an iterator over index-value pairs in the slice, traversing it backward with descending indices.

func BinarySearch

func BinarySearch[S ~[]E, E cmp.Ordered](x S, target E) (int, bool)

BinarySearch searches for target in a sorted slice and returns the earliest position where target is found, or the position where target would appear in the sort order; it also returns a bool saying whether the target is really found in the slice. The slice must be sorted in increasing order.

func BinarySearchFunc

func BinarySearchFunc[S ~[]E, E, T any](x S, target T, cmp func(E, T) int) (int, bool)

BinarySearchFunc works like BinarySearch, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp should return 0 if the slice element matches the target, a negative number if the slice element precedes the target, or a positive number if the slice element follows the target. cmp must implement the same ordering as the slice, such that if cmp(a, t) < 0 and cmp(b, t) >= 0, then a must precede b in the slice.

func Chunk

func Chunk[Slice ~[]E, E any](s Slice, n int) iter.Seq[Slice]

Chunk returns an iterator over consecutive sub-slices of up to n elements of s. All but the last sub-slice will have size n. All sub-slices are clipped to have no capacity beyond the length. If s is empty, the sequence is empty: there is no empty slice in the sequence. Chunk panics if n is less than 1.

func Chunked

func Chunked[S ~[]V, V any](s S, n int) []S

Chunked returns chunk slices of size n. The last chunk may be smaller than n.

func Clip

func Clip[S ~[]E, E any](s S) S

Clip removes unused capacity from the slice, returning s[:len(s):len(s)].

func Clone

func Clone[S ~[]E, E any](s S) S

Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone. The result may have additional unused capacity.

func Collect

func Collect[E any](seq iter.Seq[E]) []E

Collect collects values from seq into a new slice and returns it.

func Collect2

func Collect2[K, V any](seq iter.Seq2[K, V]) ([]K, []V)

Collect2 collects the elements of a sequence of pairs into two new slices, one for keys and one for values.

func Compact

func Compact[S ~[]E, E comparable](s S) S

Compact replaces consecutive runs of equal elements with a single copy. This is like the uniq command found on Unix. Compact modifies the contents of the slice s and returns the modified slice, which may have a smaller length. Compact zeroes the elements between the new length and the original length.

func CompactFunc

func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S

CompactFunc is like Compact but uses an equality function to compare elements. For runs of elements that compare equal, CompactFunc keeps the first one. CompactFunc zeroes the elements between the new length and the original length.

func Compare

func Compare[S ~[]E, E cmp.Ordered](s1 S, s2 S) int

Compare compares the elements of s1 and s2, using cmp.Compare on each pair of elements. The elements are compared sequentially, starting at index 0, until one element is not equal to the other. The result of comparing the first non-matching elements is returned. If both slices are equal until one of them ends, the shorter slice is considered less than the longer one. The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2.

func CompareFunc

func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int

CompareFunc is like Compare but uses a custom comparison function on each pair of elements. The result is the first non-zero result of cmp; if cmp always returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2), and +1 if len(s1) > len(s2).

func Concat

func Concat[S ~[]E, E any](slices ...S) S

Concat returns a new slice concatenating the passed in slices.

func Contains

func Contains[S ~[]E, E comparable](s S, v E) bool

Contains reports whether v is present in s.

func ContainsFunc

func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool

ContainsFunc reports whether at least one element e of s satisfies f(e).

func CopySeq

func CopySeq[Slice ~[]V, V any](dst Slice, src iter.Seq[V]) int

CopySeq copies elements from a sequence to a slice, returning the number of elements copied.

func Delete

func Delete[S ~[]E, E any](s S, i int, j int) S

Delete removes the elements s[i:j] from s, returning the modified slice. Delete panics if j > len(s) or s[i:j] is not a valid slice of s. Delete is O(len(s)-i), so if many items must be deleted, it is better to make a single call deleting them all together than to delete one at a time. Delete zeroes the elements s[len(s)-(j-i):len(s)].

func DeleteFunc

func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S

DeleteFunc removes any elements from s for which del returns true, returning the modified slice. DeleteFunc zeroes the elements between the new length and the original length.

func Equal

func Equal[S ~[]E, E comparable](s1 S, s2 S) bool

Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Empty and nil slices are considered equal. Floating point NaNs are not considered equal.

func EqualFunc

func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool

EqualFunc reports whether two slices are equal using an equality function on each pair of elements. If the lengths are different, EqualFunc returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first index for which eq returns false.

func Filter

func Filter[S ~[]V, V any](s S, f func(V) bool) S

Filter returns a new slice that contains only the values from s for which f returns true.

func FlatMap

func FlatMap[S ~[]VIn, VIn, VOut any](s S, f func(VIn) []VOut) []VOut

FlatMap returns a new slice that is the results of calling f on each value from s and flattening the result.

func Flatten

func Flatten[S ~[]V, V any](ss []S) S

Flatten flattens a slice of slices into a single slice.

func Grow

func Grow[S ~[]E, E any](s S, n int) S

Grow increases the slice's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the slice without another allocation. If n is negative or too large to allocate the memory, Grow panics.

func Index

func Index[S ~[]E, E comparable](s S, v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func IndexFunc

func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int

IndexFunc returns the first index i satisfying f(s[i]), or -1 if none do.

func IndexValues

func IndexValues[Slice ~[]V, V any](s Slice) iter.Seq2[int, V]

IndexValues returns a sequence of indexes alongside their values.

func Insert

func Insert[S ~[]E, E any](s S, i int, v ...E) S

Insert inserts the values v... into s at index i, returning the modified slice. The elements at s[i:] are shifted up to make room. In the returned slice r, r[i] == v[0], and, if i < len(s), r[i+len(v)] == value originally at r[i]. Insert panics if i > len(s). This function is O(len(s) + len(v)).

func IsSorted

func IsSorted[S ~[]E, E cmp.Ordered](x S) bool

IsSorted reports whether x is sorted in ascending order.

func IsSortedFunc

func IsSortedFunc[S ~[]E, E any](x S, cmp func(a E, b E) int) bool

IsSortedFunc reports whether x is sorted in ascending order, with cmp as the comparison function as defined by SortFunc.

func Map

func Map[S ~[]VIn, VIn, VOut any](s S, f func(VIn) VOut) []VOut

Map returns a new slice that is the results of calling f on each value from s.

func MapErr

func MapErr[S ~[]VIn, VIn, VOut any](s S, f func(VIn) (VOut, error)) ([]VOut, error)

func MapOK

func MapOK[S ~[]VIn, VIn, VOut any](s S, f func(VIn) (VOut, bool)) []VOut

MapOK returns a new slice that contains the results of calling f on each value from seq. If f returns false, the value is skipped.

func MapOkErr

func MapOkErr[S ~[]VIn, VIn, VOut any](s S, f func(VIn) (VOut, bool, error)) ([]VOut, error)

func Max

func Max[S ~[]E, E cmp.Ordered](x S) E

Max returns the maximal value in x. It panics if x is empty. For floating-point E, Max propagates NaNs (any NaN value in x forces the output to be NaN).

func MaxFunc

func MaxFunc[S ~[]E, E any](x S, cmp func(a E, b E) int) E

MaxFunc returns the maximal value in x, using cmp to compare elements. It panics if x is empty. If there is more than one maximal element according to the cmp function, MaxFunc returns the first one.

func Min

func Min[S ~[]E, E cmp.Ordered](x S) E

Min returns the minimal value in x. It panics if x is empty. For floating-point numbers, Min propagates NaNs (any NaN value in x forces the output to be NaN).

func MinFunc

func MinFunc[S ~[]E, E any](x S, cmp func(a E, b E) int) E

MinFunc returns the minimal value in x, using cmp to compare elements. It panics if x is empty. If there is more than one minimal element according to the cmp function, MinFunc returns the first one.

func PtrValues

func PtrValues[Slice ~[]V, V any](s Slice) iter.Seq[*V]

PtrValues returns a sequence of pointers to the elements of a slice.

func Random

func Random[Slice ~[]V, V any](slice Slice) V

Random returns a random element from the slice using `math/rand/v2`.

func Repeat

func Repeat[S ~[]E, E any](x S, count int) S

Repeat returns a new slice that repeats the provided slice the given number of times. The result has length and capacity (len(x) * count). The result is never nil. Repeat panics if count is negative or if the result of (len(x) * count) overflows.

func Replace

func Replace[S ~[]E, E any](s S, i int, j int, v ...E) S

Replace replaces the elements s[i:j] by the given v, and returns the modified slice. Replace panics if j > len(s) or s[i:j] is not a valid slice of s. When len(v) < (j-i), Replace zeroes the elements between the new length and the original length.

func Reverse

func Reverse[S ~[]E, E any](s S)

Reverse reverses the elements of the slice in place.

func Sort

func Sort[S ~[]E, E cmp.Ordered](x S)

Sort sorts a slice of any ordered type in ascending order. When sorting floating-point numbers, NaNs are ordered before other values.

func SortFunc

func SortFunc[S ~[]E, E any](x S, cmp func(a E, b E) int)

SortFunc sorts the slice x in ascending order as determined by the cmp function. This sort is not guaranteed to be stable. cmp(a, b) should return a negative number when a < b, a positive number when a > b and zero when a == b or a and b are incomparable in the sense of a strict weak ordering.

SortFunc requires that cmp is a strict weak ordering. See https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings. The function should return 0 for incomparable items.

func SortStableFunc

func SortStableFunc[S ~[]E, E any](x S, cmp func(a E, b E) int)

SortStableFunc sorts the slice x while keeping the original order of equal elements, using cmp to compare elements in the same way as SortFunc.

func Sorted

func Sorted[E cmp.Ordered](seq iter.Seq[E]) []E

Sorted collects values from seq into a new slice, sorts the slice, and returns it.

func SortedFunc

func SortedFunc[E any](seq iter.Seq[E], cmp func(E, E) int) []E

SortedFunc collects values from seq into a new slice, sorts the slice using the comparison function, and returns it.

func SortedStableFunc

func SortedStableFunc[E any](seq iter.Seq[E], cmp func(E, E) int) []E

SortedStableFunc collects values from seq into a new slice. It then sorts the slice while keeping the original order of equal elements, using the comparison function to compare elements. It returns the new slice.

func TryAppendSeq

func TryAppendSeq[Slice ~[]V, V any](s Slice, seq iter.Seq2[V, error]) (Slice, error)

TryAppendSeq appends the elements of a sequence to a slice, stopping at the first error.

func TryCollect

func TryCollect[V any](seq iter.Seq2[V, error]) ([]V, error)

TryCollect collects the elements of a sequence into a new slice, stopping at the first error.

func TryCopySeq

func TryCopySeq[Slice ~[]V, V any](dst Slice, src iter.Seq2[V, error]) (int, error)

TryCopySeq copies elements from a sequence to a slice, stopping at the first error. It returns the number of elements copied and the error.

func Unique

func Unique[S ~[]V, V comparable](s S) S

Unique returns a new slice with all duplicate elements removed.

func UniqueFunc

func UniqueFunc[S ~[]V, V any, Key comparable](s S, f func(V) Key) S

UniqueFunc returns a new slice that contains only the unique values from s, using the given function to generate the key for uniqueness checks.

func Values

func Values[Slice ~[]E, E any](s Slice) iter.Seq[E]

Values returns an iterator that yields the slice elements in order.

Types

This section is empty.

Jump to

Keyboard shortcuts

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