Documentation
¶
Overview ¶
Package slices contains common utilities to work on slices of any type.
Index ¶
- func AllMatch[T any](s []T, pred func(v T) bool) bool
- func Diff[S ~[]T, T comparable](a, b S) []T
- func Map[In, Out any](in []In, fn func(In) Out) []Out
- func MapIter[In, Out any](s iter.Seq[In], fn func(In) Out) iter.Seq[Out]
- func SortedUnique[S ~[]T, T cmp.Ordered](s S) S
- func SubsetOf[S ~[]T, T comparable](a, b S) (bool, []T)
- func Unique[S ~[]T, T comparable](s S) S
- func UniqueFunc[S ~[]T, T any, K comparable](s S, key func(i int) K) S
- func XorNil[T any](s1, s2 []T) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllMatch ¶ added in v1.16.5
AllMatch returns true if pred is true for each element in s, false otherwise. May not evaluate on all elements if not necessary for determining the result. If the slice is empty then true is returned and predicate is not evaluated.
func Diff ¶
func Diff[S ~[]T, T comparable](a, b S) []T
Diff returns a slice of elements which is the difference of a and b. The returned slice keeps the elements in the same order found in the "a" slice. Both input slices are considered as sets, that is, all elements are considered as unique when computing the difference.
func Map ¶ added in v1.19.0
func Map[In, Out any](in []In, fn func(In) Out) []Out
Map returns a slice obtained applying fn over the input elements.
func MapIter ¶ added in v1.19.0
MapIter returns an iterator obtained applying fn over the input elements.
func SortedUnique ¶
SortedUnique sorts and dedup the input slice in place. It uses the < operator to compare the elements in the slice and thus requires the elements to satisfies contraints.Ordered.
func SubsetOf ¶
func SubsetOf[S ~[]T, T comparable](a, b S) (bool, []T)
SubsetOf returns a boolean that indicates if slice a is a subset of slice b. In case it is not, the returned slice contains all the unique elements that are in a but not in b.
func Unique ¶
func Unique[S ~[]T, T comparable](s S) S
Unique deduplicates the elements in the input slice, preserving their ordering and modifying the slice in place. Unique relies on a map to find multiple occurrences of the same elements. For slices with a size less than 192 elements, a simpler O(N^2) search algorithm that does not allocate memory is used instead. Limit of 192 has been experimentally derived (look at BenchmarkUnique for more information).
func UniqueFunc ¶
func UniqueFunc[S ~[]T, T any, K comparable](s S, key func(i int) K) S
UniqueFunc deduplicates the elements in the input slice like Unique, but takes a function to extract the comparable "key" to compare T. This is slower than Unique, but can be used with non-comparable elements.
Types ¶
This section is empty.