slices

package
v0.0.0-...-a5f0885 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: MIT Imports: 2 Imported by: 21

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch[E constraints.Ordered](x []E, target E) int

BinarySearch searches for target in a sorted slice and returns the smallest index at which target is found. If the target is not found, the index at which it could be inserted into the slice is returned; therefore, if the intention is to find target itself a separate check for equality with the element at the returned index is required.

func BinarySearchFunc

func BinarySearchFunc[E any](x []E, ok func(E) bool) int

BinarySearchFunc uses binary search to find and return the smallest index i in [0, n) at which ok(i) is true, assuming that on the range [0, n), ok(i) == true implies ok(i+1) == true. That is, BinarySearchFunc requires that ok is false for some (possibly empty) prefix of the input range [0, n) and then true for the (possibly empty) remainder; BinarySearchFunc returns the first true index. If there is no such index, BinarySearchFunc returns n. (Note that the "not found" return value is not -1 as in, for instance, strings.Index.) Search calls ok(i) only for i in the range [0, n).

func Clip

func Clip[S ~[]E, E any](s S) S

Clip removes unused capacity from the slice, returning s[:len(s):len(s)].

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 Compact

func Compact[S ~[]E, E comparable](s S) S

Compact replaces consecutive runs of equal elements with a single copy. This is like the uniq command found on Unix. Compact modifies the contents of the slice s; it does not create a new slice. Intended usage is to assign the result back to the input slice.

func CompactFunc

func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S

CompactFunc is like Compact but uses a comparison function.

func Compare

func Compare[E constraints.Ordered](s1, s2 []E) int

Compare compares the elements of s1 and s2. The elements are compared sequentially, starting at index 0, until one element is not equal to the other. The result of comparing the first non-matching elements is returned. If both slices are equal until one of them ends, the shorter slice is considered less than the longer one. The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2. Comparisons involving floating point NaNs are ignored.

func CompareFunc

func CompareFunc[E1, E2 any](s1 []E1, s2 []E2, cmp func(E1, E2) int) int

CompareFunc is like Compare but uses a comparison function on each pair of elements. The elements are compared in increasing index order, and the comparisons stop after the first time cmp returns non-zero. The result is the first non-zero result of cmp; if cmp always returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2), and +1 if len(s1) > len(s2).

func Concat

func Concat[T any](slice []T, values ...T) []T

Similar to Append but we leave the original slice untouched and return a new slice

func Contains

func Contains[E comparable](s []E, v E) bool

Contains reports whether v is present in s.

func ContainsFunc

func ContainsFunc[T any](slice []T, f func(T) bool) bool

func Delete

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

Delete removes the elements s[i:j] from s, returning the modified slice. Delete panics if s[i:j] is not a valid slice of s. Delete modifies the contents of the slice s; it does not create a new slice. Delete is O(len(s)-(j-i)), so if many items must be deleted, it is better to make a single call deleting them all together than to delete one at a time.

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 any](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 Every

func Every[T any](slice []T, test func(T) bool) bool

func Filter

func Filter[T any](slice []T, test func(T) bool) []T

Produces a new slice, leaves the input slice untouched.

func FilterInPlace

func FilterInPlace[T any](slice []T, test func(T) bool) []T

Mutates original slice. Intended usage is to reassign the slice result to the input slice.

func FilterMap

func FilterMap[T any, E any](slice []T, test func(T) (E, bool)) []E

Produces a new slice, leaves the input slice untouched.

func FilterMapWithIndex

func FilterMapWithIndex[T any, E any](slice []T, test func(T, int) (E, bool)) []E

func FilterWithIndex

func FilterWithIndex[T any](slice []T, f func(T, int) bool) []T

Produces a new slice, leaves the input slice untouched.

func Find

func Find[T any](slice []T, f func(T) bool) (T, bool)

func FindMap

func FindMap[T any, V any](slice []T, f func(T) (V, bool)) (V, bool)

Sometimes you need to find an element and then map it to some other value based on information you obtained while finding it. This function lets you do that

func FlatMap

func FlatMap[T any, V any](slice []T, f func(T) []V) []V

Produces a new slice, leaves the input slice untouched.

func FlatMapWithIndex

func FlatMapWithIndex[T any, V any](slice []T, f func(T, int) []V) []V

func Flatten

func Flatten[T any](slice [][]T) []T

func ForEach

func ForEach[T any](slice []T, f func(T))

func ForEachWithIndex

func ForEachWithIndex[T any](slice []T, f func(T, int))

func Grow

func Grow[S ~[]E, E any](s S, n int) S

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. Grow may modify elements of the slice between the length and the capacity. If n is negative or too large to allocate the memory, Grow panics.

func Index

func Index[E comparable](s []E, v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func IndexFunc

func IndexFunc[E any](s []E, f func(E) bool) int

IndexFunc returns the first index i satisfying f(s[i]), or -1 if none do.

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. In the returned slice r, r[i] == v[0]. Insert panics if i is out of range. This function is O(len(s) + len(v)).

func IsSorted

func IsSorted[E constraints.Ordered](x []E) bool

IsSorted reports whether x is sorted in ascending order.

func IsSortedFunc

func IsSortedFunc[E any](x []E, less func(a, b E) bool) bool

IsSortedFunc reports whether x is sorted in ascending order, with less as the comparison function.

func Map

func Map[T any, V any](slice []T, f func(T) V) []V

Produces a new slice, leaves the input slice untouched.

func MapInPlace

func MapInPlace[T any](slice []T, f func(T) T)

func MapWithIndex

func MapWithIndex[T any, V any](slice []T, f func(T, int) V) []V

Produces a new slice, leaves the input slice untouched.

func MaxBy

func MaxBy[T any, V constraints.Ordered](slice []T, f func(T) V) V

func MinBy

func MinBy[T any, V constraints.Ordered](slice []T, f func(T) V) V

func Move

func Move[T any](slice []T, fromIndex int, toIndex int) []T

Operates on the input slice. Expected use is to reassign the result to the input slice.

func Partition

func Partition[T any](slice []T, test func(T) bool) ([]T, []T)

func Pop

func Pop[T any](slice []T) (T, []T)

Pops item from the end of the slice and returns it, along with the updated slice Mutates original slice. Intended usage is to reassign the slice result to the input slice.

func Prepend

func Prepend[T any](slice []T, values ...T) []T

Prepends items to the beginning of a slice. E.g. Prepend([]int{1,2}, 3, 4) = []int{3,4,1,2} Mutates original slice. Intended usage is to reassign the slice result to the input slice.

func Remove

func Remove[T any](slice []T, index int) []T

Removes the element at the given index. Intended usage is to reassign the result to the input slice.

func Reverse

func Reverse[T any](slice []T) []T

Produces a new slice, leaves the input slice untouched

func ReverseInPlace

func ReverseInPlace[T any](slice []T)

func Shift

func Shift[T any](slice []T) (T, []T)

Shifts item from the beginning of the slice and returns it, along with the updated slice. Mutates original slice. Intended usage is to reassign the slice result to the input slice.

func Some

func Some[T any](slice []T, test func(T) bool) bool

func Sort

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

Sort sorts a slice of any ordered type in ascending order.

func SortFunc

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

Sort sorts the slice x in ascending order as determined by the less function. This sort is not guaranteed to be stable.

func SortStableFunc

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

SortStable sorts the slice x while keeping the original order of equal elements, using less to compare elements.

func TryFilter

func TryFilter[T any](slice []T, test func(T) (bool, error)) ([]T, error)

func TryFilterMap

func TryFilterMap[T any, E any](slice []T, test func(T) (E, bool, error)) ([]E, error)

func TryFilterMapWithIndex

func TryFilterMapWithIndex[T any, E any](slice []T, test func(T, int) (E, bool, error)) ([]E, error)

func TryFilterWithIndex

func TryFilterWithIndex[T any](slice []T, test func(T, int) (bool, error)) ([]T, error)

func TryForEach

func TryForEach[T any](slice []T, f func(T) error) error

func TryForEachWithIndex

func TryForEachWithIndex[T any](slice []T, f func(T, int) error) error

func TryMap

func TryMap[T any, V any](slice []T, f func(T) (V, error)) ([]V, error)

func TryMapWithIndex

func TryMapWithIndex[T any, V any](slice []T, f func(T, int) (V, error)) ([]V, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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