Documentation ¶
Overview ¶
Package zslice implements generic functions for slices.
Index ¶
- func AppendCopy[T any](s []T, app T, more ...T) []T
- func Choose[T any](list []T) T
- func ContainsAny[T comparable](list []T, find ...T) bool
- func Copy[T any](src []T, len, cap int) []T
- func Difference[T comparable](set []T, others ...[]T) []T
- func Intersect[T comparable](a, b []T) []T
- func IsUniq[T cmp.Ordered](list []T) bool
- func Longest(list []string) int
- func LongestFunc[T any](list []T, f func(T) string) int
- func Max[T cmp.Ordered](list []T) T
- func Min[T cmp.Ordered](list []T) T
- func Remove[T comparable](l *[]T, name T) bool
- func RemoveIndexes[T any](l *[]T, indexes ...int)
- func Repeat[T any](s T, n int) []T
- func SameElements[T cmp.Ordered](a, b []T) bool
- func Shuffle[T any](list []T)
- func Uniq[T comparable](list []T) []T
- func UniqSort[T cmp.Ordered](list []T) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendCopy ¶
func AppendCopy[T any](s []T, app T, more ...T) []T
AppendCopy is like append(), but ensures the new value is always a copy.
The len and cap will always be set to exactly the len and cap of the new array.
func ContainsAny ¶
func ContainsAny[T comparable](list []T, find ...T) bool
ContainsAny reports whether any of the strings are in the list
func Copy ¶
Copy a slice.
This is like slices.Clone, but you can set a len and cap for the new slice; this can be larger than the src slice to prevent copying the array if you're appending more items later, or lower if you want to copy and truncate the array.
Like make(), this will panic if len > cap.
func Difference ¶
func Difference[T comparable](set []T, others ...[]T) []T
Difference returns a new slice with elements that are in "set" but not in "others".
func Intersect ¶
func Intersect[T comparable](a, b []T) []T
Intersect returns a new slice with elements that are in both "a" and "b".
func LongestFunc ¶
LongestFunc gets the longest string value in this list.
for example to get the longest email address from a []mail.Address:
zslice.LongestFunc(addrs, func(s mail.Address) string { return s.Address })
func Remove ¶
func Remove[T comparable](l *[]T, name T) bool
Remove all values from a list.
The return value indicates if this value was found at all.
func RemoveIndexes ¶
RemoveIndexes removes all the given indexes.
The indexes is expected to be sorted from lowest to highest.
Will panic on out of bounds.
func SameElements ¶
SameElements reports if the two slices have the same elements.
This is similar to slices.Equal, but don't take order in to account.
func Shuffle ¶
func Shuffle[T any](list []T)
Shuffle randomizes the order of values.
This uses math/rand, and is not "true random".
func Uniq ¶
func Uniq[T comparable](list []T) []T
Uniq removes duplicate entry from the list.
The order will be preserved, and the first item will be kept. This is slower than UniqSort().
Types ¶
This section is empty.