slice

package
v0.0.48 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 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 added in v0.0.38

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

ClearZero creates a slice with all zero values removed.

func ClearZeroRef added in v0.0.38

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 added in v0.0.30

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

Contains reports whether slice contains any one of target elements.

func ContainsRef added in v0.0.47

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 added in v0.0.44

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, the original slice will not be modified.

func DeleteElems added in v0.0.44

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 added in v0.0.45

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 added in v0.0.45

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 added in v0.0.45

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 Indexes implemented by generics and be recommended to use.

func IndexesFunc added in v0.0.46

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

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

func IndexesRef added in v0.0.45

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

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

func Insert added in v0.0.45

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 function Insert, the original slice will not be modified.

func InsertRef added in v0.0.45

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 added in v0.0.38

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 added in v0.0.30

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 added in v0.0.47

func MaxRef(a any) any

MaxRef returns the largest element of the Slice or Array.

func MaxRefE added in v0.0.47

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 added in v0.0.30

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 added in v0.0.47

func MinRef(a any) any

MinRef returns the smallest element of the Slice or Array.

func MinRefE added in v0.0.47

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 added in v0.0.45

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 added in v0.0.45

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 added in v0.0.45

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 added in v0.0.30

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 added in v0.0.46

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 added in v0.0.30

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 added in v0.0.46

func SumRef(a any) float64

SumRef sum slice or array elements.

func SumRefE added in v0.0.46

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 added in v0.0.30

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.go.dev/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 added in v0.0.47

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