Documentation
¶
Overview ¶
Package slicesext provides extra functionality on top of the slices package.
Index ¶
- func Copy[T any](s []T) []T
- func Count[T any](s []T, f func(T) bool) int
- func CountError[T any](s []T, f func(T) (bool, error)) (int, error)
- func Duplicates[T comparable](s []T) []T
- func ElementsContained(superset []string, subset []string) bool
- func ElementsEqual[T comparable](one []T, two []T) bool
- func Filter[T any](s []T, f func(T) bool) []T
- func FilterError[T any](s []T, f func(T) (bool, error)) ([]T, error)
- func Map[T1, T2 any](s []T1, f func(T1) T2) []T2
- func MapError[T1, T2 any](s []T1, f func(T1) (T2, error)) ([]T2, error)
- func MapKeysToSlice[K comparable, V any](m map[K]V) []K
- func MapKeysToSortedSlice[M ~map[K]V, K Ordered, V any](m M) []K
- func Reduce[T1, T2 any](s []T1, f func(T2, T1) T2, initialValue T2) T2
- func ReduceError[T1, T2 any](s []T1, f func(T2, T1) (T2, error), initialValue T2) (T2, error)
- func ToChunks[T any](s []T, chunkSize int) [][]T
- func ToStructMap[T comparable](s []T) map[T]struct{}
- func ToUniqueSorted[S ~[]T, T Ordered](s S) S
- func ToValuesMapV[K comparable, V any](s []V, f func(V) K) map[K]V
- type Ordered
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountError ¶
CountError returns the number of elements in s where f returns true.
Returns error the first time f returns error.
func Duplicates ¶
func Duplicates[T comparable](s []T) []T
Duplicates returns the duplicate values in s.
Values are returned in the order they are found in S.
If an element is the zero value, it is not added to duplicates.
func ElementsContained ¶
ElementsContained returns true if superset contains subset.
Nil and empty slices are treated as equals.
func ElementsEqual ¶
func ElementsEqual[T comparable](one []T, two []T) bool
ElementsEqual returns true if the two slices have equal elements.
Nil and empty slices are treated as equals.
func FilterError ¶
FilterError filters the slice to only the values where f returns true.
Returns error the first time f returns error.
func MapKeysToSlice ¶
func MapKeysToSlice[K comparable, V any](m map[K]V) []K
MapKeysToSlice converts the map's keys to a slice.
func MapKeysToSortedSlice ¶
MapKeysToSortedSlice converts the map's keys to a sorted slice.
func Reduce ¶
func Reduce[T1, T2 any](s []T1, f func(T2, T1) T2, initialValue T2) T2
Reduce reduces the slice.
func ToChunks ¶
ToChunks splits s into chunks of the given chunk size.
If s is nil or empty, returns empty. If chunkSize is <=0, returns [][]T{s}.
func ToStructMap ¶
func ToStructMap[T comparable](s []T) map[T]struct{}
ToStructMap converts the slice to a map with struct{} values.
func ToUniqueSorted ¶
func ToUniqueSorted[S ~[]T, T Ordered](s S) S
ToUniqueSorted returns a sorted copy of s with no duplicates.
func ToValuesMapV ¶
func ToValuesMapV[K comparable, V any](s []V, f func(V) K) map[K]V
ToValuesMap transforms the input slice into a map from f(V) -> V.
If f(V) is the zero value of K, nothing is added to the map.
Duplicate values of type K will result in a single map entry.
Types ¶
type Ordered ¶
type Ordered interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string }
Ordered matches cmp.Ordered until we only support Go versions >= 1.21.
TODO: remove and replace with cmp.Ordered when we only support Go versions >= 1.21.