slice

package
v0.0.54 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearZero

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

ClearZero creates a slice with all zero values removed.

func ClearZeroRef

func ClearZeroRef(a any) any

ClearZeroRef creates a slice with all zero values removed. ClearZeroRef will panic is argument a isn't a slice.

func Contains

func Contains[E comparable](s []E, targets ...E) bool

Contains reports whether slice contains any one of target elements.

func ContainsRef

func ContainsRef(a any, targets ...any) bool

ContainsRef reports whether slice contains any one of target elements. Note that if the target elements are a numeric literal, please specify their type explicitly, otherwise type defaults to int. E.g. you might call like ContainsRef([]int32{1,2,3}, int32(1)). ContainsRef will panic if argument a isn't slice.

func Delete

func Delete[S ~[]E, E any](s S, indexes ...int) S

Delete removes the specified indexes elements from the slice. Unlike the standard library function Delete, Delete won't modify the original slice.

func DeleteElems

func DeleteElems[S ~[]E, E comparable](s S, elms ...E) S

DeleteElems removes the specified elements from slice. Note that the original slice will not be modified.

func DeleteElemsRef

func DeleteElemsRef(a any, elms ...any) any

DeleteElemsRef removes the specified elements from slice implemented by reflect. Note that the original slice will not be modified.

func DeleteRef

func DeleteRef(a any, indexes ...int) any

DeleteRef removes the elements specified by indexes from the slice. Note that the original slice will not be modified.

func Indexes

func Indexes[E comparable](s []E, v E) []int

Indexes returns the specified element all indexes. Indexes implemented by generics has a better performance than IndexesRef implemented by reflect, so Indexes is recommended to be used while not IndexesRef.

func IndexesFunc

func IndexesFunc[E any](s []E, f func(E) bool) []int

IndexesFunc returns the specified element all indexes satisfying f(s[i]).

func IndexesRef

func IndexesRef(a any, value any) []int

IndexesRef returns the specified element all indexes from a slice or array with returned error.

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 and returns the result slice. Unlike the standard library, Insert won't modify the original slice.

func InsertRef

func InsertRef(a any, i int, v ...any) any

InsertRef inserts elements to slice in the specified index implemented by reflect. Note that the original slice will not be modified.

func Make

func Make[E any](e E, size ...int) []E

Make makes a slice with specified element, len and capacity. If there is no size argument to call Make, e.g. Make(1) will return a int slice []int{1} only including one specified element, The first size argument represents the slice length. A second size argument may be provided to specify a different capacity. From above description we can know that Make(e) is equal to Make(e, 1) and Make(e, 1, 1).

func Max

func Max[T constraints.Ordered](s []T) T

Max returns the largest element of the slice. Max implemented by generics is recomended to be used.

func MaxRef

func MaxRef(a any) any

MaxRef returns the largest element of the Slice or Array.

func MaxRefE

func MaxRefE(a any) (any, error)

MaxRefE returns the largest element of the Slice or Array. MaxRefE will panic if argument a isn't Slice or Array.

func Min

func Min[T constraints.Ordered](s []T) T

Min returns the smallest element of the slice and an error if occurred. Min implemented by generics is recomended to be used.

func MinRef

func MinRef(a any) any

MinRef returns the smallest element of the Slice or Array.

func MinRefE

func MinRefE(a any) (any, error)

MinRefE returns the smallest element of the Slice or Array. MinRefE will panic if argument a isn't Slice or Array.

func RandomElem

func RandomElem(a any) any

RandomElem returns a random element from a Slice or Array. If the length of Slice or Array is zero it will panic.

func Replace

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

Replace replaces the elements s[i:j] by the given v. The resulting slice len is equal to the original slice. Replace panics if s[i:j] is not a valid slice of s. Note that the original slice will not be modified.

func ReplaceRef

func ReplaceRef(a any, i, j int, v ...any) any

ReplaceRef modifies the specified index elements of slice implemented by reflect. The resulting slice len is equal to the original slice. ReplaceRef panics if s[i:j] is not a valid slice of s. Note that the original slice will not be modified.

func Reverse

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

Reverse reverses the specified slice without modifying the original slice. Reverse implemented by generics is recommended to be used.

func ReverseRef

func ReverseRef(a any) any

ReverseRef implemented by reflect reverses the specified slice without modifying the original slice. ReverseRef will panic if argument a isn't Slice.

func Sum

func Sum[S ~[]E, E numerical](s S) float64

Sum calculates the sum of slice elements. Sum implemented by generics is recommended to be used.

func SumRef

func SumRef(a any) float64

SumRef sum slice or array elements.

func SumRefE

func SumRefE(a any) (float64, error)

SumRefE returns the sum of slice or array elements implemented by reflect. E.g. input []int32{1, 2, 3} and output is 6. SumRefE will panic if argument a's Kind isn't Slice, Array or String. If a's Kind is String, SumRefE will sum characters encoding in byte.

func Unique

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

Unique replaces repeated elements with a single copy and returns a new slice. Unique is like the experimental lib function https://pkg.golang.ir/golang.org/x/exp/slices#Compact, but Unique do not modify the original slice and the original slice also doesn't need to be sorted. Unique implemented by generics is recommended to be used. E.g. input []int{1, 2, 3, 2} and output is []int{1, 2, 3}.

func UniqueRef

func UniqueRef(a any) any

UniqueRef replaces repeated elements with a single copy and returns a new slice. The original slice will not be modified.

Types

This section is empty.

Jump to

Keyboard shortcuts

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