Documentation ¶
Overview ¶
Package slice provides functions for manipulating slices.
Index ¶
- func Chunk[S ~[]E, E any](slice S, size int) []S
- func Clone[S ~[]E, E any](s S) S
- func Concat[S ~[]E, E any](ss ...S) S
- func Count[S ~[]E, E any](s S, f func(E) bool) int
- func Dedup[S ~[]E, E comparable](s S) S
- func Equals[S ~[]E, E comparable](a, b S) bool
- func EqualsAnyOrder[S ~[]E, E comparable](a, b S) bool
- func Every[S ~[]E, E any](s S, f func(E) bool) bool
- func Expand2D[SS ~[]S, S ~[]E, E any](ss SS, m, n int) SS
- func Fill[S ~[]E, E any](s S, v E)
- func FillRange[S ~[]E, E any](s S, v E, start, end int)
- func Filter[S ~[]E, E any](s S, f func(E) bool) S
- func FilterMap[S ~[]E, E, R any](s S, filterFunc func(E) bool, mapFunc func(E) R) []R
- func Find[S ~[]E, E any](s S, f func(E) bool) (E, bool)
- func FindDefault[S ~[]E, E any](s S, f func(E) bool, defaultValue E) E
- func FindIndex[S ~[]E, E any](s S, f func(E) bool) int
- func FindLast[S ~[]E, E any](s S, f func(E) bool) (E, bool)
- func FindLastDefault[S ~[]E, E any](s S, f func(E) bool, defaultValue E) E
- func FindLastIndex[S ~[]E, E any](s S, f func(E) bool) int
- func Flat[SS ~[]S, S ~[]E, E any](s SS) S
- func ForEach[S ~[]E, E any](s S, f func(E))
- func ForEachIndex[S ~[]E, E any](s S, f func(v E, i int))
- func Group[S ~[]E, E any, K comparable](s S, f func(E) K) map[K]S
- func GroupMap[S ~[]E, E any, K comparable, V any](s S, keyFunc func(E) K, mapFunc func(E) V) map[K][]V
- func Includes[S ~[]E, E comparable](s S, v E) bool
- func IndexOf[S ~[]E, E comparable](s S, v E) int
- func IndexOfFrom[S ~[]E, E comparable](s S, v E, from int) int
- func Insert[S ~[]E, E any](s S, i int, v E) S
- func Keys[M ~map[K]V, K comparable, V any](m M) []K
- func LastIndexOf[S ~[]E, E comparable](s S, v E) int
- func Make2D[T any](m, n int) [][]T
- func Map[S ~[]E, E, R any](s S, f func(E) R) []R
- func Most[S ~[]E, E any](s S, f func(v, most E) bool) E
- func Move[S ~[]E, E any](s S, a, b int)
- func NoNil[S ~[]E, E any](s S) S
- func Pop[T any](s []T) (T, []T)
- func Push[T any](s []T, v T) []T
- func Random[S ~[]E, E any](s S) (E, S)
- func Range[T constraints.Integer](start, end T) []T
- func Reduce[S ~[]E, E any, R any](s S, f func(v E, acc R) R, init R) R
- func ReduceRight[S ~[]E, E, R any](s S, f func(v E, acc R) R, init R) R
- func Remove[S ~[]E, E comparable](s S, v E) S
- func RemoveFunc[S ~[]E, E any](s S, f func(E) bool) S
- func RemoveIndex[S ~[]E, E any](s S, i int) S
- func Reverse[S ~[]E, E any](s S)
- func ReverseCopy[S ~[]E, E any](s S) S
- func Shift[T any](s []T) (T, []T)
- func Shuffle[S ~[]E, E any](s S)
- func Some[S ~[]E, E any](s S, f func(E) bool) bool
- func Sort[S ~[]E, E any](s S, less func(a, b E) bool)
- func Sum[S ~[]E, E Number](s S) E
- func Unshift[T any](s []T, v T) []T
- func Values[M ~map[K]V, K comparable, V any](m M) []V
- type Number
- type Zipped
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶ added in v0.5.0
Chunk returns a new slice of slices where each slice contains at most the given size number of elements from the original slice.
func Clone ¶ added in v0.6.0
func Clone[S ~[]E, E any](s S) S
Clone returns a copy of the input slice.
func Concat ¶ added in v0.5.0
func Concat[S ~[]E, E any](ss ...S) S
Concat returns a new slice containing all the elements of the input slices in order.
func Count ¶ added in v0.5.0
Count returns the number of elements in the slice that satisfy the given function.
func Dedup ¶
func Dedup[S ~[]E, E comparable](s S) S
Dedup returns a new slice containing only the unique elements of the input slice.
func Equals ¶
func Equals[S ~[]E, E comparable](a, b S) bool
Equals returns true if the two slices contain the same elements in the same order.
func EqualsAnyOrder ¶
func EqualsAnyOrder[S ~[]E, E comparable](a, b S) bool
EqualsAnyOrder returns true if the two slices contain the same elements, regardless of order.
func Expand2D ¶ added in v0.4.0
Expand2D expands a 2D slice ss to m rows and n columns, adding elements to any short rows and appending any short columns.
func Filter ¶
Filter returns a new slice containing only the elements from the original slice that satisfy the given function.
func FilterMap ¶ added in v0.5.0
FilterMap returns a new slice containing the elements of the input slice that satisfy filterFunc, transformed by mapFunc.
func Find ¶
Find returns the first element in the slice that satisfies the given function, along with a boolean indicating whether such an element was found.
func FindDefault ¶
FindDefault returns the first element in s that satisfies the predicate f. If no such element is found, it returns defaultValue.
func FindIndex ¶
FindIndex returns the index of the first element in the slice that satisfies the given function, or -1 if no such element was found.
func FindLast ¶
FindLast returns the last element in the slice that satisfies the given function, along with a boolean indicating whether such an element was found.
func FindLastDefault ¶
FindLastDefault returns the last element in s that satisfies the predicate f. If no such element is found, it returns defaultValue.
func FindLastIndex ¶
FindLastIndex returns the index of the last element in the slice that satisfies the given function, or -1 if no such element was found.
func Flat ¶
func Flat[SS ~[]S, S ~[]E, E any](s SS) S
Flat flattens a slice of slices into a single slice.
func ForEach ¶
func ForEach[S ~[]E, E any](s S, f func(E))
ForEach applies the given function to every element in the slice.
func ForEachIndex ¶
ForEachIndex applies the given function to every element in the slice along with its index.
func Group ¶
func Group[S ~[]E, E any, K comparable](s S, f func(E) K) map[K]S
Group groups the elements of a slice by applying a given function to each element and using the result as a key in a map.
func GroupMap ¶ added in v0.5.0
func GroupMap[S ~[]E, E any, K comparable, V any](s S, keyFunc func(E) K, mapFunc func(E) V) map[K][]V
GroupMap groups the elements of a slice by applying a key function and a map function to each element, using the result of the key function as a key in a map and the result of the map function as a value in a slice.
func Includes ¶
func Includes[S ~[]E, E comparable](s S, v E) bool
Includes returns true if the given value is found in the slice, otherwise false.
func IndexOf ¶
func IndexOf[S ~[]E, E comparable](s S, v E) int
IndexOf returns the index of the first occurrence of the given value in the slice, or -1 if not found.
func IndexOfFrom ¶
func IndexOfFrom[S ~[]E, E comparable](s S, v E, from int) int
IndexOfFrom returns the index of the first occurrence of the given value in the slice, starting from the given index, or -1 if not found.
func Keys ¶
func Keys[M ~map[K]V, K comparable, V any](m M) []K
Keys returns a slice of keys extracted from the input map m. The keys are returned in no particular order. It returns nil if the map is empty.
func LastIndexOf ¶
func LastIndexOf[S ~[]E, E comparable](s S, v E) int
LastIndexOf returns the index of the last occurrence of the given value in the slice, or -1 if not found.
func Map ¶
func Map[S ~[]E, E, R any](s S, f func(E) R) []R
Map applies the given function to every element in the slice and returns a new slice containing the results.
func Most ¶
Most returns the element in the slice that satisfies the given function and is "most" according to that function. The definition of "most" is left up to the caller of this function.
func NoNil ¶ added in v0.3.0
func NoNil[S ~[]E, E any](s S) S
NoNil returns the input slice with a non-nil value. If the input slice is nil, it returns a new empty slice of the same type.
func Pop ¶
func Pop[T any](s []T) (T, []T)
Pop removes and returns the last element of a slice, along with the remaining slice. If the slice is empty, Pop returns a zero value and a nil slice.
func Push ¶
func Push[T any](s []T, v T) []T
Push adds an element to the end of a slice and returns the resulting slice.
func Random ¶
func Random[S ~[]E, E any](s S) (E, S)
Random returns a random element from a slice and a new slice with the randomly selected element removed. If the input slice is empty, it returns a zero value and a nil slice.
func Range ¶ added in v0.3.0
func Range[T constraints.Integer](start, end T) []T
Range returns a slice of integers from start (inclusive) to end (exclusive). If start is greater than or equal to end, an empty slice is returned.
func Reduce ¶
Reduce applies a function to each element of a slice, accumulating the result into an initial value.
func ReduceRight ¶
func ReduceRight[S ~[]E, E, R any](s S, f func(v E, acc R) R, init R) R
ReduceRight applies a function to each element of a slice in reverse order, accumulating the result into an initial value.
func Remove ¶ added in v0.3.0
func Remove[S ~[]E, E comparable](s S, v E) S
Remove returns a new slice with the first occurrence of v removed from s. If v is not found in s, Remove returns s unchanged.
func RemoveFunc ¶ added in v0.3.0
RemoveFunc returns a new slice with the first element e in s for which f(e) is true removed. If no such element is found, RemoveFunc returns s unchanged.
func RemoveIndex ¶ added in v0.3.0
RemoveIndex returns a new slice with the element at index i removed from s. If i is out of bounds for s, RemoveIndex returns s unchanged.
func ReverseCopy ¶
func ReverseCopy[S ~[]E, E any](s S) S
ReverseCopy returns a new slice with the elements in reverse order.
func Shift ¶
func Shift[T any](s []T) (T, []T)
Shift removes and returns the first element of a slice, along with the remaining slice. If the slice is empty, Shift returns a zero value and a nil slice.
func Shuffle ¶ added in v0.3.0
func Shuffle[S ~[]E, E any](s S)
Shuffle randomizes the order of elements in the given slice using rand.Shuffle. Note that the function modifies the original slice, and does not return a new one.
func Sort ¶ added in v0.2.0
Sort sorts the elements of slice s in increasing order, according to the order defined by the less function. The less function returns true if the first element should be ordered before the second element.
func Sum ¶
func Sum[S ~[]E, E Number](s S) E
Sum returns the sum of all elements in a slice of type T.
func Unshift ¶
func Unshift[T any](s []T, v T) []T
Unshift adds an element to the beginning of a slice and returns the resulting slice.
func Values ¶
func Values[M ~map[K]V, K comparable, V any](m M) []V
Values returns a slice of values extracted from the input map m. The values are returned in no particular order. It returns nil if the map is empty.
Types ¶
type Number ¶
type Number interface { constraints.Complex | constraints.Float | constraints.Integer }
Number is a type constraint that allows only complex, float, and integer types.
type Zipped ¶ added in v0.5.0
type Zipped[A, B any] struct { A A B B }