asg

package
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 4 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Anys added in v1.0.15

func Anys[T any](s []T) []any

Anys convert slice 's' to []any slice

func Clip

func Clip[T any](s []T) []T

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

func Clone

func Clone[T any](s []T, n ...int) []T

Clone returns a copy of the slice with additional +n cap. The elements are copied using assignment, so this is a shallow clone.

func Compact

func Compact[T comparable](s []T) []T

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 s smaller length. When Compact discards m elements in total, it might not modify the elements s[len(s)-m:len(s)]. If those elements contain pointers you might consider zeroing those elements so that objects they reference can be garbage collected.

func CompactFunc

func CompactFunc[T any](s []T, eq func(T, T) bool) []T

CompactFunc is like Compact but uses an equality function to compare elements. For runs of elements that compare equal, CompactFunc keeps the first one.

func Compare added in v1.2.9

func Compare[T cmp.Ordered](s1, s2 []T) 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 added in v1.2.9

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

func Concat[T any](ss ...[]T) []T

Concat returns s new slice concatenating the passed in slices.

func Contains

func Contains[T comparable](s []T, v T) bool

Contains reports whether the v is contained in the slice s.

func ContainsAll added in v1.2.9

func ContainsAll[T comparable](s []T, cs ...T) bool

ContainsAll reports whether all elements of cs are contained in the slice s.

func ContainsAny added in v1.0.27

func ContainsAny[T comparable](s []T, cs ...T) bool

ContainsAny reports whether at least one element of cs is contained in the slice s.

func ContainsFunc

func ContainsFunc[T any](s []T, f func(T) bool) bool

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

func Delete

func Delete[T any](s []T, i, j int) []T

Delete removes the elements s[i:j] from s, returning the modified slice. Delete panics if s[i:j] is not a valid slice of s. Delete is O(len(s)-j), 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 might not modify the elements s[len(s)-(j-i):len(s)]. If those elements contain pointers you might consider zeroing those elements so that objects they reference can be garbage collected.

func DeleteEqual added in v1.0.13

func DeleteEqual[T comparable](s []T, e T) []T

DeleteEqual removes any elements from s for which elemant == e, returning the modified slice. When DeleteFunc removes m elements, it might not modify the elements s[len(s)-m:len(s)]. If those elements contain pointers you might consider zeroing those elements so that objects they reference can be garbage collected.

func DeleteFunc

func DeleteFunc[T any](s []T, del func(T) bool) []T

DeleteFunc removes any elements from s for which del returns true, returning the modified slice. When DeleteFunc removes m elements, it might not modify the elements s[len(s)-m:len(s)]. If those elements contain pointers you might consider zeroing those elements so that objects they reference can be garbage collected.

func Equal

func Equal[T comparable](a []T, b []T) bool

Equal reports whether a and b are the same length and contain the same element. A nil argument is equivalent to an empty slice.

func EqualFunc

func EqualFunc[A any, B any](a []A, b []B, eq func(A, B) 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 FindFunc added in v1.0.27

func FindFunc[T any](s []T, f func(T) bool) (v T, ok bool)

FindFunc returns the first item satisfying f(s[i]), or (zero,false) if none do.

func First added in v1.2.3

func First[T any](s []T, d ...T) (v T)

First get first element of a slice s. returns first value of `d...` if slice `s` is empty. returns zero value if slice `s` and `d` is empty.

func Get

func Get[T any](s []T, i int) (v T, ok bool)

Get get element at the specified index i.

func Grow

func Grow[T any](s []T, n int) []T

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[T comparable](s []T, v T) int

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

func IndexFunc

func IndexFunc[T any](s []T, f func(T) bool) int

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

func Insert added in v1.2.11

func Insert[T any](s []T, i int, v ...T) []T

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)). If the result is empty, it has the same nilness as s.

func Join added in v1.0.27

func Join[T any](elems []T, sep string, fmt ...func(T) string) string

Join concatenates the elements of its first argument to create a single string. The separator string sep is placed between elements in the resulting string.

func Last added in v1.2.4

func Last[T any](s []T) (v T)

Last get last element of slice s. returns zero value if slice is empty.

func Max added in v1.0.21

func Max[T cmp.Ordered](x []T) T

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

func MaxFunc[T any](x []T, cmp func(a, b T) int) T

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

func Min[T cmp.Ordered](x []T) T

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

func MinFunc[T any](x []T, cmp func(a, b T) int) T

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 Replace added in v1.2.11

func Replace[T any](s []T, i, j int, v ...T) []T

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. If the result is empty, it has the same nilness as s.

func Reverse

func Reverse[T any](s []T)

Reverse reverses the elements of the slice in place.

func SliceOf added in v1.2.8

func SliceOf[T any](v ...T) []T

SliceOf returns []T{v[0], v[1], ...}

Types

This section is empty.

Jump to

Keyboard shortcuts

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