Documentation ¶
Overview ¶
Package slicesext provides extra functionality on top of the slices package.
Index ¶
- func Concat[S ~[]E, E any](slices ...S) S
- 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 Deduplicate[V comparable](s []V) []V
- func DeduplicateAny[K comparable, V any](s []V, f func(V) K) []V
- func Duplicates[T comparable](s []T) []T
- func ElementsContained[T comparable](superset []T, subset []T) 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 Grow[S ~[]E, E any](s S, n int) S
- func IndexedToSortedValues[T any](s []Indexed[T]) []T
- func IndexedToValues[T any](s []Indexed[T]) []T
- 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 cmp.Ordered, V any](m M) []K
- func MapValuesToSlice[K comparable, V any](m map[K]V) []V
- func MapValuesToSortedSlice[K comparable, V cmp.Ordered](m map[K]V) []V
- 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 ToIndexedValuesMap[K comparable, V any](values []V, f func(V) K) map[K][]Indexed[V]
- func ToString[S ~[]T, T fmt.Stringer](s S) string
- func ToStructMap[T comparable](s []T) map[T]struct{}
- func ToStructMapOmitEmpty[T comparable](s []T) map[T]struct{}
- func ToUniqueIndexedValuesMap[K comparable, V any](values []V, f func(V) K) (map[K]Indexed[V], error)
- func ToUniqueIndexedValuesMapError[K comparable, V any](values []V, f func(V) (K, error)) (map[K]Indexed[V], error)
- func ToUniqueSorted[S ~[]T, T cmp.Ordered](s S) S
- func ToUniqueValuesMap[K comparable, V any](s []V, f func(V) K) (map[K]V, error)
- func ToUniqueValuesMapError[K comparable, V any](s []V, f func(V) (K, error)) (map[K]V, error)
- func ToValuesMap[K comparable, V any](s []V, f func(V) K) map[K][]V
- func ToValuesMapError[K comparable, V any](s []V, f func(V) (K, error)) (map[K][]V, error)
- type Indexed
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Concat ¶ added in v1.33.0
func Concat[S ~[]E, E any](slices ...S) S
Concat returns a new slice concatenating the passed in slices.
TODO FUTURE: Delete this in favor of slices.Concat.
func Copy ¶
func Copy[T any](s []T) []T
Copy returns a copy of the slice.
TODO FUTURE: Delete this in favor of slices.Clone.
func CountError ¶
CountError returns the number of elements in s where f returns true.
Returns error the first time f returns error.
func Deduplicate ¶ added in v1.32.0
func Deduplicate[V comparable](s []V) []V
Deduplicate returns the unique values of s.
func DeduplicateAny ¶ added in v1.32.0
func DeduplicateAny[K comparable, V any](s []V, f func(V) K) []V
Deduplicate returns the unique values of s when transformed with f.
Earlier occurrences of a value are returned and later occurrences are dropped.
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 ¶
func ElementsContained[T comparable](superset []T, subset []T) bool
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 Grow ¶ added in v1.33.0
Grow increases the slice's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the slice without another allocation. If n is negative or too large to allocate the memory, Grow panics.
TODO FUTURE: Delete this in favor of slices.Grow.
func IndexedToSortedValues ¶ added in v1.32.0
IndexedToSortedValues takes the indexed values and returns them as values sorted by index.
func IndexedToValues ¶ added in v1.32.0
IndexedToSortedValues takes the indexed values and returns them as values.
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 MapValuesToSlice ¶ added in v1.32.0
func MapValuesToSlice[K comparable, V any](m map[K]V) []V
MapValuesToSlice converts the map's values to a slice.
Duplicate values will be added. This should generally be used in cases where you know there is a 1-1 mapping from K to V.
func MapValuesToSortedSlice ¶ added in v1.32.0
func MapValuesToSortedSlice[K comparable, V cmp.Ordered](m map[K]V) []V
MapValuesToSlice converts the map's values to a sorted slice.
Duplicate values will be added. This should generally be used in cases where you know there is a 1-1 mapping from K to V.
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 ToIndexedValuesMap ¶ added in v1.32.0
func ToIndexedValuesMap[K comparable, V any](values []V, f func(V) K) map[K][]Indexed[V]
ToIndexedValuesMap calls ToValuesMap on the indexed values.
func ToStructMap ¶
func ToStructMap[T comparable](s []T) map[T]struct{}
ToStructMap converts the slice to a map with struct{} values.
func ToStructMapOmitEmpty ¶ added in v1.32.0
func ToStructMapOmitEmpty[T comparable](s []T) map[T]struct{}
ToStructMapOmitEmpty converts the slice to a map with struct{} values.
Zero values of T are not added to the map.
TODO FUTURE: Make ToStructMap use this logic, remove ToStructMapOmitEmpty, to match other functions.
func ToUniqueIndexedValuesMap ¶ added in v1.32.0
func ToUniqueIndexedValuesMap[K comparable, V any](values []V, f func(V) K) (map[K]Indexed[V], error)
ToUniqueIndexedValuesMap calls ToUniqueValuesMap on the indexed values.
func ToUniqueIndexedValuesMapError ¶ added in v1.32.0
func ToUniqueIndexedValuesMapError[K comparable, V any](values []V, f func(V) (K, error)) (map[K]Indexed[V], error)
ToUniqueIndexedValuesMapError calls ToUniqueValuesMapError on the indexed values.
func ToUniqueSorted ¶
ToUniqueSorted returns a sorted copy of s with no duplicates.
func ToUniqueValuesMap ¶ added in v1.32.0
func ToUniqueValuesMap[K comparable, V any](s []V, f func(V) K) (map[K]V, error)
ToUniqueValuesMap 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 an error.
func ToUniqueValuesMapError ¶ added in v1.32.0
func ToUniqueValuesMapError[K comparable, V any](s []V, f func(V) (K, error)) (map[K]V, error)
ToUniqueValuesMapError 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 an error. Otherwise returns error the first time f returns error.
func ToValuesMap ¶ added in v1.32.0
func ToValuesMap[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.
func ToValuesMapError ¶ added in v1.32.0
func ToValuesMapError[K comparable, V any](s []V, f func(V) (K, error)) (map[K][]V, error)
ToValuesMapError 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.
Returns error the first time f returns error.