Documentation
¶
Index ¶
- func Chunk[T any](slice []T, chunkSize int) [][]T
- func ClearZero[S ~[]E, E comparable](s S) S
- func ClearZeroRef(a any) any
- func Contains[E comparable, S ~[]E](s S, targets ...E) bool
- func ContainsAll[E comparable, S ~[]E](s, elems S) bool
- func ContainsRef(a any, targets ...any) bool
- func Delete[S ~[]E, E any](s S, indexes ...int) S
- func DeleteElems[S ~[]E, E comparable](s S, elms ...E) S
- func DeleteElemsRef(a any, elms ...any) any
- func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S
- func DeleteRef(a any, indexes ...int) any
- func Diff[T comparable](ls, rs []T) []T
- func DiffFunc[T any, K comparable](ls, rs []T, getKey func(e T) K) []T
- func Distinct[E comparable, S ~[]E](s S) S
- func DistinctFunc[E any, K comparable, S ~[]E](s S, getKey func(item E) K) S
- func DistinctRef(a any) any
- func ExtractField[S, E any](s []S, fieldName string) ([]E, error)
- func FilterFunc[T any](data []T, retain func(T) bool) []T
- func GroupFunc[E any, K comparable, S ~[]E](s S, getKey func(E) K) map[K]S
- func Indexes[E comparable](s []E, v E) []int
- func IndexesFunc[E any](s []E, f func(E) bool) []int
- func IndexesRef(a any, value any) []int
- func Insert[S ~[]E, E any](s S, i int, v ...E) S
- func InsertRef(a any, i int, v ...any) any
- func Intersection[T comparable](ls, rs []T) []T
- func IntersectionFunc[T any, K comparable](s1, s2 []T, getKey func(e T) K) []T
- func Make[E any](e E, size ...int) []E
- func Map[T, U any](data []T, f func(T) U) []U
- func Max[T cmp.Ordered](s []T) T
- func MaxRef(a any) any
- func MaxRefE(a any) (any, error)
- func Merge[T any](ss ...[]T) []T
- func Min[T cmp.Ordered](s []T) T
- func MinRef(a any) any
- func MinRefE(a any) (any, error)
- func RandomElem(a any) any
- func Replace[S ~[]E, E any](s S, i, j int, v ...E) S
- func ReplaceRef(a any, i, j int, v ...any) any
- func Reverse[E any, S ~[]E](s S) S
- func ReverseRef(a any) any
- func Sum[S ~[]E, E numerical](s S) E
- func SumRef(a any) float64
- func SumRefE(a any) (float64, error)
- func SymDiff[T comparable](s1, s2 []T) []T
- func SymDiffFunc[T any, K comparable](s1, s2 []T, getKey func(e T) K) []T
- func ToMap[E any, K comparable, S ~[]E](s S, getKey func(e E) K) map[K]E
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 ¶
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](s S, targets ...E) bool
Contains reports whether slice contains any one of target elements.
func ContainsAll ¶ added in v0.0.62
func ContainsAll[E comparable, S ~[]E](s, elems S) bool
ContainsAll checks whether all given elements exist in the target slice.
func ContainsRef ¶
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 ¶
Delete removes the specified indexes elements from the slice. Note that the original slice will not be modified.
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 ¶
DeleteElemsRef removes the specified elements from slice implemented by reflect. Note that the original slice will not be modified.
func DeleteFunc ¶ added in v0.0.61
DeleteFunc removes any elements from s for which del returns true, returning the new slice. Unlike the standard lib func DeleteFunc, this func won't modify the original slice.
func DeleteRef ¶
DeleteRef removes the elements specified by indexes from the slice. Note that the original slice will not be modified.
func Diff ¶ added in v0.0.55
func Diff[T comparable](ls, rs []T) []T
Diff computes the difference of left slice relative to right slice.
func DiffFunc ¶ added in v0.0.55
func DiffFunc[T any, K comparable](ls, rs []T, getKey func(e T) K) []T
DiffFunc computes the difference of set1 relative to set2 based on a specified key extraction function.
func Distinct ¶ added in v0.0.55
func Distinct[E comparable, S ~[]E](s S) S
Distinct returns a new slice containing only the unique elements from the input slice. T is the type of the input slice elements, and it must be comparable.
func DistinctFunc ¶ added in v0.0.55
func DistinctFunc[E any, K comparable, S ~[]E](s S, getKey func(item E) K) S
DistinctFunc returns a new slice containing only the unique elements from the input slice, based on a key extraction function that determines the uniqueness of each element.
func DistinctRef ¶ added in v0.0.61
DistinctRef returns a new slice containing only the unique elements from the input slice. T is the type of the input slice elements, and it must be comparable. Deprecated: plz use generic func Distinct first.
func ExtractField ¶ added in v0.0.61
ExtractField takes a slice of structs and a field name, and returns a slice of values for that field.
func FilterFunc ¶ added in v0.0.61
FilterFunc filters out elements that do not meet the conditions.
func GroupFunc ¶ added in v0.0.55
func GroupFunc[E any, K comparable, S ~[]E](s S, getKey func(E) K) map[K]S
GroupFunc groups elements of the slice based on a key extraction function. T is the type of the input slice elements, and U is the type of the keys. The keys must be comparable, meaning they can be used as map keys.
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 ¶
IndexesFunc returns the specified element all indexes satisfying f(s[i]).
func IndexesRef ¶
IndexesRef returns the specified element all indexes from a slice or array with returned error.
func Insert ¶
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 ¶
InsertRef inserts elements to slice in the specified index implemented by reflect. Note that the original slice will not be modified.
func Intersection ¶ added in v0.0.55
func Intersection[T comparable](ls, rs []T) []T
Intersection get the intersection of two slice elements.
func IntersectionFunc ¶ added in v0.0.55
func IntersectionFunc[T any, K comparable](s1, s2 []T, getKey func(e T) K) []T
IntersectionFunc get the intersection of two slice elements, based on a key extraction function that determines the equality of each element.
func Make ¶
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 Map ¶ added in v0.0.55
func Map[T, U any](data []T, f func(T) U) []U
Map applies a transformation function to each element of the input slice and returns a new slice containing the results. T is the type of the input slice elements, and U is the type of the output slice elements.
func Max ¶
Max returns the largest element of the slice. Max implemented by generics is recomended to be used.
func MaxRefE ¶
MaxRefE returns the largest element of the Slice or Array. MaxRefE will panic if argument a isn't Slice or Array.
func Merge ¶ added in v0.0.55
func Merge[T any](ss ...[]T) []T
Merge takes multiple slices of type T and combines them into a single slice. T can be any type, allowing this function to work with slices of different data types.
func Min ¶
Min returns the smallest element of the slice and an error if occurred. Min implemented by generics is recomended to be used.
func MinRefE ¶
MinRefE returns the smallest element of the Slice or Array. MinRefE will panic if argument a isn't Slice or Array.
func RandomElem ¶
RandomElem returns a random element from a Slice or Array. If the length of Slice or Array is zero it will panic.
func Replace ¶
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 ¶
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 ¶
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) E
Sum calculates the sum of slice elements. Sum implemented by generics is recommended to be used.
func SumRefE ¶
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 SymDiff ¶ added in v0.0.55
func SymDiff[T comparable](s1, s2 []T) []T
SymDiff computes the symmetric difference between two slices of type T.
func SymDiffFunc ¶ added in v0.0.55
func SymDiffFunc[T any, K comparable](s1, s2 []T, getKey func(e T) K) []T
SymDiffFunc computes the symmetric difference between two slices of type T, based on a key extraction function that determines the equality of each element.
func ToMap ¶ added in v0.0.59
func ToMap[E any, K comparable, S ~[]E](s S, getKey func(e E) K) map[K]E
ToMap converts a slice of type T into a map, using a specified key extraction function. T can be any type, and K is the type of the keys used in the resulting map, which must be comparable.
Types ¶
This section is empty.