Documentation
¶
Overview ¶
Package slices provides functions for working with Go slices.
Index ¶
- func Adjust[T any](sl []T, length, capacity int) []T
- func All[T any](sl []T, pred Predicate[T]) bool
- func Any[T any](sl []T, pred Predicate[T]) bool
- func Clone[T any](sl []T) []T
- func Collect[T any](seq iter.Seq[T]) []T
- func Concat[T any](sls ...[]T) []T
- func Contains[T comparable](sl []T, v T) bool
- func ContainsFunc[T any](sl []T, v T, f CmpFunc[T]) bool
- func Create[T any](vs ...T) []T
- func CreateFunc[T any](length int, fn func() T) []T
- func Delete[T any](sl []T, idx ...int) []T
- func DeletePred[T any](sl []T, pred Predicate[T]) []T
- func DeleteRange[T any](sl []T, start, length int) []T
- func DropWhile[T any](sl []T, pred Predicate[T]) []T
- func Equal[T comparable](sl1, sl2 []T) bool
- func EqualFunc[T any](sl1, sl2 []T, f CmpFunc[T]) bool
- func Extrema[T Ordered](sl []T) (min, max T)
- func ExtremaFunc[T Ordered](sl []T, f LessFunc[T]) (min, max T)
- func Fill[T any](sl []T, v T)
- func Filter[T any](sl []T, pred Predicate[T]) []T
- func FilterIndex[T any](sl []T, pred Predicate[int]) []T
- func Find[T comparable](sl []T, v T) int
- func FindAll[T comparable](sl []T, v T) []int
- func FindAllFunc[T any](sl []T, v T, f CmpFunc[T]) []int
- func FindFrom[T comparable](sl []T, v T, start int) int
- func FindFromFunc[T any](sl []T, v T, start int, f CmpFunc[T]) int
- func FindFunc[T any](sl []T, v T, f CmpFunc[T]) int
- func Flatten[T any](sl [][]T) []T
- func Get[T any](sl []T, idx int) T
- func Group[T comparable](sl []T) [][]T
- func GroupFunc[T any](sl []T, f CmpFunc[T]) [][]T
- func Insert[T any](sl []T, idx int, v ...T) []T
- func IsSorted[T Ordered](sl []T) bool
- func IsSortedFunc[T any](sl []T, f LessFunc[T]) bool
- func Iter[T any](sl []T) iter.Seq2[int, T]
- func Map[T, U any](sl []T, f MapFunc[T, U]) []U
- func Map2[T, U, V any](sl1 []T, sl2 []U, f Map2Func[T, U, V]) []V
- func Map3[T, U, V, W any](sl1 []T, sl2 []U, sl3 []V, f Map3Func[T, U, V, W]) []W
- func Max[T Ordered](sl []T) T
- func MaxFunc[T any](sl []T, f LessFunc[T]) T
- func Min[T Ordered](sl []T) T
- func MinFunc[T any](sl []T, f LessFunc[T]) T
- func Product[T Number](sl []T) T
- func Purge[T comparable](sl, ps []T) []T
- func PurgeFunc[T any](sl, ps []T, f CmpFunc[T]) []T
- func RandomFloat(length int) []float64
- func RandomInt(length, n int) []int
- func Range[T Signed | Float](start, end, step T) []T
- func Reduce[T, U any](sl []T, init U, f ReduceFunc[T, U]) U
- func Repeat[T any](v T, n int) []T
- func RepeatSlice[T any](sl []T, n int) []T
- func Reverse[T any](sl []T)
- func ReverseClone[T any](sl []T) []T
- func Search[T Ordered](sl []T, v T) (idx int, found bool)
- func SearchFunc[T any](sl []T, v T, f LessFunc[T]) (int, bool)
- func Shuffle[T any](sl []T)
- func Slice[T any](sl []T, start, end int) []T
- func Sort[T Ordered](sl []T)
- func SortClone[T Ordered](sl []T) []T
- func SortCloneFunc[T any](sl []T, stable bool, f LessFunc[T]) []T
- func SortFunc[T any](sl []T, stable bool, f LessFunc[T])
- func Sum[T Number](sl []T) T
- func TakeWhile[T any](sl []T, pred Predicate[T]) []T
- func Unique[T comparable](sl []T) []T
- func UniqueFunc[T any](sl []T, f CmpFunc[T]) []T
- func Values[T any](sl []T) iter.Seq[T]
- type CmpFunc
- type Float
- type LessFunc
- type Map2Func
- type Map3Func
- type MapFunc
- type Number
- type Ordered
- type Predicate
- type ReduceFunc
- type Signed
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Adjust ¶ added in v0.6.0
Adjust adjusts length and capacity of the given slice. If capacity < length it is set to length. Panics if length < 0.
func All ¶ added in v0.2.0
All returns true if for all values in sl the function pred returns true. Returns true for an empty slice. If pred is nil, type T must be bool.
func Any ¶ added in v0.2.0
Any returns true if for any value in sl the function pred returns true. Returns false for an empty slice. If pred is nil, type T must be bool.
func Concat ¶
func Concat[T any](sls ...[]T) []T
Concat returns the concatenation of all given slices as a new slice.
func Contains ¶
func Contains[T comparable](sl []T, v T) bool
Contains returns true if the value is in the slice.
func ContainsFunc ¶
ContainsFunc returns true if the value is in the slice using a function to compare values.
func CreateFunc ¶ added in v0.5.0
CreateFunc returns a slice of given length with values created by successive calls to fn.
func Delete ¶ added in v0.3.0
Delete deletes the values at the given indexes and returns the modified slice.
func DeletePred ¶ added in v0.3.0
DeletePred deletes the values for which pred returns true and returns the modified slice.
func DeleteRange ¶ added in v0.3.0
DeleteRange deletes length values beginning at start and returns the modified slice.
func DropWhile ¶ added in v0.3.0
DropWhile drops values from sl as long as pred returns true and returns the rest in a new slice.
func Equal ¶
func Equal[T comparable](sl1, sl2 []T) bool
Equal returns true if the slices are equal.
func Extrema ¶ added in v0.7.0
func Extrema[T Ordered](sl []T) (min, max T)
Extrema returns minimum and maximum from the slice. Panics if the slice is empty or nil.
func ExtremaFunc ¶ added in v0.7.0
ExtremaFunc returns minimum and maximum from the slice using a function to compare values. Panics if the slice is empty or nil.
func Fill ¶ added in v0.3.0
func Fill[T any](sl []T, v T)
Fill fills the slice with the given value including the unused capacity.
func FilterIndex ¶ added in v0.8.0
FilterIndex returns a new slice with all values for which pred(index) returns true.
func Find ¶
func Find[T comparable](sl []T, v T) int
Find returns the index of the value in sl or -1 if not found.
func FindAll ¶ added in v0.3.0
func FindAll[T comparable](sl []T, v T) []int
FindAll returns all indexes of the value v.
func FindAllFunc ¶ added in v0.3.0
FindAll returns all indexes of the value v using a function to compare values.
func FindFrom ¶ added in v0.3.0
func FindFrom[T comparable](sl []T, v T, start int) int
FindFrom returns the index of the value in sl or -1 if not found. It starts at index start.
func FindFromFunc ¶ added in v0.3.0
FindFromFunc returns the index of the value in sl or -1 if not found using a function to compare values. It starts at index start.
func FindFunc ¶
FindFunc returns the index of the value in sl or -1 if not found using a function to compare values.
func Flatten ¶
func Flatten[T any](sl [][]T) []T
Flatten returns a new slice with one level of nesting removed from a slice of slices.
func Get ¶ added in v0.4.0
Get gets the element at index idx. If idx < 0 it computes the index from the end. Panics if idx >= len(sl) or the computed index is < 0.
func Group ¶ added in v0.3.0
func Group[T comparable](sl []T) [][]T
Group groups consecutive, equal values from sl.
func GroupFunc ¶ added in v0.3.0
GroupFunc groups consecutive, equal values from sl using a function to compare values.
func Insert ¶ added in v0.3.0
Insert inserts values at index idx and returns the modified slice. If there is enough unused capacity, the original slice will be modified.
func IsSortedFunc ¶
IsSortedFunc returns true if the slice is sorted using a function to compare values.
func Map2 ¶ added in v0.2.0
Map2 maps values in two slices using a mapping function. It stops when the shortest slice is exhaustet.
func Map3 ¶ added in v0.2.0
Map3 maps values in three slices using a mapping function. It stops when the shortest slice is exhaustet.
func Max ¶
func Max[T Ordered](sl []T) T
Max returns the maximum from the slice. Panics if the slice is empty or nil.
func MaxFunc ¶
MaxFunc returns the maximum from the slice using a function to compare values. Panics if the slice is empty or nil.
func Min ¶
func Min[T Ordered](sl []T) T
Min returns the minimum from the slice. Panics if the slice is empty or nil.
func MinFunc ¶
MinFunc returns the minimum from the slice using a function to compare values. Panics if the slice is empty or nil.
func Product ¶
func Product[T Number](sl []T) T
Product returns the product of the values in a slice.
func Purge ¶ added in v0.3.0
func Purge[T comparable](sl, ps []T) []T
Purge returns a new slice with all values from sl which are not in ps.
func PurgeFunc ¶ added in v0.3.0
PurgeFunc returns a new slice with all values from sl which are not in ps using a function to compare values.
func RandomFloat ¶
RandomFloat returns a slice of given length with pseudo-random values in the interval [0.0,1.0).
func RandomInt ¶
RandomInt returns a slice of given length with pseudo-random values in the interval [0,n). n == 0 means no upper limit. Panics when n < 0
func Reduce ¶ added in v0.2.0
func Reduce[T, U any](sl []T, init U, f ReduceFunc[T, U]) U
Reduce reduces sl to a single value by accumulating the values in sl using function f. The first argument to f is the accumulated value (starting with init).
func RepeatSlice ¶ added in v0.5.0
Repeat returns a slice with n repetitions of the values in the given slice. Pancis if n < 0.
func ReverseClone ¶
func ReverseClone[T any](sl []T) []T
ReverseClone returns a clone of the slice with the order of values reversed.
func Search ¶ added in v0.2.0
Search does a binary search and returns idx and found. If found is true, idx is the first index at which v was found. If found is false, idx is the index at which v must be inserted to keep sl sorted. The slice sl must be sorted in ascending order.
func SearchFunc ¶ added in v0.2.0
SearchFunc does a binary search using a function to compare values. If found is true, idx is the first index at which v was found. If found is false, idx is the index at which v must be inserted to keep sl sorted. The slice sl must be sorted in ascending order according to function f.
func Shuffle ¶
func Shuffle[T any](sl []T)
Shuffle pseudo-randomizes the order of the elements in the slice.
func Slice ¶ added in v0.4.0
Slice returns a slice from sl. If start or end < 0 it computes the indexes from the end. Panics if the (computed) slice bounds are out of range.
func SortClone ¶
func SortClone[T Ordered](sl []T) []T
SortClone returns a sorted clone of the slice.
func SortCloneFunc ¶
SortCloneFunc returns a sorted clone of the slice using a function to compare values.
func TakeWhile ¶ added in v0.3.0
TakeWhile takes values from sl as long as pred returns true and returns them in a new slice.
func UniqueFunc ¶
UniqueFunc returns the unique values from a slice using a function to compare values.
Types ¶
type LessFunc ¶
A LessFunc compares two values and returns true if the first is less then the second.
type Map2Func ¶ added in v0.2.0
type Map2Func[T, U, V any] func(x T, y U) V
A Map2Func maps two values to another value.
type Map3Func ¶ added in v0.2.0
type Map3Func[T, U, V, W any] func(x T, y U, z V) W
A Map3Func maps three values to another value.
type MapFunc ¶
type MapFunc[T, U any] func(x T) U
A MapFunc maps a value to another value which may have a different type.