slices

package
v0.0.0-...-af29bc0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2024 License: Apache-2.0 Imports: 3 Imported by: 4

Documentation

Overview

Package slices defines various functions useful with slices of any type.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch[S ~[]E, E cmp.Ordered](x S, target E) (int, bool)

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 Delete

func Delete[S ~[]E, E any](s S, i int) S

Delete removes the element i from s, returning the modified slice.

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

func Filter[E any](s []E, f func(E) bool) []E

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

func FilterInPlace[E any](s []E, keep func(E) bool) []E

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 FindFunc

func FindFunc[E any](s []E, f func(E) bool) *E

FindFunc finds the first element matching the function, or nil if none do

func First

func First[E any](s []E) *E

First returns the first item in the slice, if there is one

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

func Insert[S ~[]E, E any](s S, i int, v ...E) S

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 Join

func Join(sep string, fields ...string) string

func Map

func Map[E any, O any](s []E, f func(E) O) []O

Map runs f() over all elements in s and returns the result

func MapErr

func MapErr[E any, O any](s []E, f func(E) (O, error)) ([]O, error)

MapErr runs f() over all elements in s and returns the result, short circuiting if there is an error.

func MapFilter

func MapFilter[E any, O any](s []E, f func(E) *O) []O

MapFilter runs f() over all elements in s and returns any non-nil results

func Max

func Max[S ~[]E, E cmp.Ordered](x S) E

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 Reverse

func Reverse[E any](r []E) []E

Reverse returns its argument array reversed

func Sort

func Sort[E cmp.Ordered](x []E) []E

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

func SortBy[E any, A cmp.Ordered](x []E, extract func(a E) A) []E

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

func SortFunc[E any](x []E, less func(a, b E) int) []E

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

func SortStableFunc[E any](x []E, less func(a, b E) int) []E

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL