Documentation ¶
Overview ¶
Package slices defines various functions useful with slices of any type.
Index ¶
- func BinarySearch[S ~[]E, E cmp.Ordered](x S, target E) (int, bool)
- func Clone[S ~[]E, E any](s S) S
- func Contains[E comparable](s []E, v E) bool
- func Delete[S ~[]E, E any](s S, i int) S
- func Dereference[E any](s []*E) []E
- func Equal[E comparable](s1, s2 []E) bool
- func EqualFunc[E1, E2 comparable](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool
- func EqualUnordered[E comparable](s1, s2 []E) bool
- func Filter[E any](s []E, f func(E) bool) []E
- func FilterDuplicatesPresorted[E comparable](s []E) []E
- func FilterInPlace[E any](s []E, keep func(E) bool) []E
- func FindFunc[E any](s []E, f func(E) bool) *E
- func First[E any](s []E) *E
- func Flatten[E any](s [][]E) []E
- func Group[T any, K comparable](data []T, f func(T) K) map[K][]T
- func GroupUnique[T any, K comparable](data []T, f func(T) K) map[K]T
- func Insert[S ~[]E, E any](s S, i int, v ...E) S
- func Join(sep string, fields ...string) string
- func Map[E any, O any](s []E, f func(E) O) []O
- func MapErr[E any, O any](s []E, f func(E) (O, error)) ([]O, error)
- func MapFilter[E any, O any](s []E, f func(E) *O) []O
- func Max[S ~[]E, E cmp.Ordered](x S) E
- func Reference[E any](s []E) []*E
- func Reverse[E any](r []E) []E
- func Sort[E cmp.Ordered](x []E) []E
- func SortBy[E any, A cmp.Ordered](x []E, extract func(a E) A) []E
- func SortFunc[E any](x []E, less func(a, b E) int) []E
- func SortStableFunc[E any](x []E, less func(a, b E) int) []E
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶
func Clone[S ~[]E, E any](s S) S
Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone.
func Contains ¶
func Contains[E comparable](s []E, v E) bool
Contains reports whether v is present in s.
func Dereference ¶
func Dereference[E any](s []*E) []E
Dereference returns all non-nil references, dereferenced
func Equal ¶
func Equal[E comparable](s1, s2 []E) bool
Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.
func EqualFunc ¶
func EqualFunc[E1, E2 comparable](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool
EqualFunc reports whether two slices are equal using a comparison function on each pair of elements. If the lengths are different, EqualFunc returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first index for which eq returns false.
func EqualUnordered ¶
func EqualUnordered[E comparable](s1, s2 []E) bool
EqualUnordered reports whether two slices are equal, ignoring order
func Filter ¶
Filter retains all elements in []E that f(E) returns true for. A new slice is created and returned. Use FilterInPlace to perform in-place
func FilterDuplicatesPresorted ¶
func FilterDuplicatesPresorted[E comparable](s []E) []E
FilterDuplicatesPresorted retains all unique elements in []E. The slices MUST be pre-sorted.
func FilterInPlace ¶
FilterInPlace retains all elements in []E that keep(E) returns true for. The array is *mutated in place* and returned. Use Filter to avoid mutation
func Flatten ¶
func Flatten[E any](s [][]E) []E
Flatten merges a slice of slices into a single slice.
func Group ¶
func Group[T any, K comparable](data []T, f func(T) K) map[K][]T
Group groups a slice by a key.
func GroupUnique ¶
func GroupUnique[T any, K comparable](data []T, f func(T) K) map[K]T
GroupUnique groups a slice by a key. Each key must be unique or data will be lost. To allow multiple use Group.
func Insert ¶
Insert inserts the values v... into s at index i, returning the modified slice. The elements at s[i:] are shifted up to make room. In the returned slice r, r[i] == v[0], and r[i+len(v)] == value originally at r[i]. Insert panics if i is out of range. This function is O(len(s) + len(v)).
func MapErr ¶
MapErr runs f() over all elements in s and returns the result, short circuiting if there is an error.
func Max ¶
Max returns the maximal value in x. It panics if x is empty. For floating-point E, Max propagates NaNs (any NaN value in x forces the output to be NaN).
func Reference ¶
func Reference[E any](s []E) []*E
Reference takes a pointer to all elements in the slice
func Sort ¶
Sort sorts a slice of any ordered type in ascending order. The slice is modified in place but returned.
Example ¶
// ExampleSort shows the best practices in sorting by multiple keys. // If you just have one key, use SortBy // Test has 3 values; we will sort by them in Rank < First < Last order type Test struct { Rank int First string Last string } l := []Test{ {0, "b", "b"}, {0, "b", "a"}, {1, "a", "a"}, {0, "c", "a"}, {1, "c", "a"}, {0, "a", "a"}, {2, "z", "a"}, } SortFunc(l, func(a, b Test) int { if r := cmp.Compare(a.Rank, b.Rank); r != 0 { return r } if r := cmp.Compare(a.First, b.First); r != 0 { return r } return cmp.Compare(a.Last, b.Last) }) fmt.Println(l)
Output: [{0 a a} {0 b a} {0 b b} {0 c a} {1 a a} {1 c a} {2 z a}]
func SortBy ¶
SortBy is a helper to sort a slice by some value. Typically, this would be sorting a struct by a single field. If you need to have multiple fields, see the ExampleSort.
func SortFunc ¶
SortFunc sorts the slice x in ascending order as determined by the less function. This sort is not guaranteed to be stable. The slice is modified in place but returned.
func SortStableFunc ¶
SortStableFunc sorts the slice x while keeping the original order of equal element. The slice is modified in place but returned. Please refer to SortFunc for usage instructions.
Types ¶
This section is empty.