Documentation
¶
Overview ¶
Package xslices provide missing functionality to the slices package.
It was created before the standard slices package, so some functionality may be duplicate.
Index ¶
- Constants
- func At[T any](slice []T, index int) T
- func Close[T interface{ ... }](e0, e1 any) bool
- func Copy[T any](slice []T) []T
- func DeepSliceCmp(s0, s1 any, cmpFn func(e0, e1 any) bool) bool
- func EqualAny[T comparable](e0, e1 any) bool
- func FillAnySlice(slice any, value any)
- func FillSlice[T any](slice []T, value T)
- func Flag[T any](name string, defaultValue []T, usage string, ...) *[]T
- func Gather[T any](values []T, indices []int) []T
- func Iota[T interface{ ... }](start T, len int) (slice []T)
- func Keys[K comparable, V any](m map[K]V) []K
- func Last[T any](slice []T) T
- func Map[In, Out any](in []In, fn func(e In) Out) (out []Out)
- func MapParallel[In, Out any](in []In, fn func(e In) Out) (out []Out)
- func Max[T cmp.Ordered](slice []T) (max T)
- func Min[T cmp.Ordered](slice []T) (min T)
- func MultidimensionalSliceWithValue[T any](value T, dims ...int) any
- func MustSlicesInRelData[T float32 | float64](s0, s1 []T, relDelta float64) error
- func Pop[T any](slice []T) (T, []T)
- func SetAt[T any](slice []T, index int, value T)
- func SetLast[T any](slice []T, value T)
- func Slice2DWithValue[T any](value T, dim0, dim1 int) [][]T
- func Slice3DWithValue[T any](value T, dim0, dim1, dim2 int) [][][]T
- func SliceToGoStr(slice any) string
- func SliceWithValue[T any](size int, value T) []T
- func SlicesInDelta(s0, s1 any, delta float64) bool
- func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K
Constants ¶
const Epsilon = 1e-4
Variables ¶
This section is empty.
Functions ¶
func At ¶
At takes an element at the given `index`, where `index` can be negative, in which case it takes from the end of the slice.
func Copy ¶
func Copy[T any](slice []T) []T
Copy creates a new (shallow) copy of T. A shortcut to a call to `make` and then `copy`.
func DeepSliceCmp ¶
DeepSliceCmp returns false if the slices given are of different shapes, or if the given cmpFn on each element returns false.
func EqualAny ¶
func EqualAny[T comparable](e0, e1 any) bool
EqualAny is a comparison function that tests for exact equality and can be fed to DeepSliceCmp.
func FillAnySlice ¶
FillAnySlice fills a slice with the given value. Both are given as interface{} values, so it works for arbitrary underlying type values. Silently returns if the slice is not a slice or if the value is not the base type of the slice.
func FillSlice ¶
func FillSlice[T any](slice []T, value T)
FillSlice with fill the slice with the given value.
func Flag ¶
func Flag[T any](name string, defaultValue []T, usage string, parserFn func(valueStr string) (T, error)) *[]T
Flag creates a flag for []T with the given name, description and default value. It takes as input a parser for an individual T value.
func Gather ¶
Gather returns a slice of T values by gathering the values indexed by indices. Eg: Gather([]int{1,3}, []float32{10, 20, 30, 40, 50}) -> []float32{20, 40}
func Iota ¶
func Iota[T interface {
constraints.Integer | constraints.Float
}](start T, len int) (slice []T)
Iota returns a slice of incremental int values, starting with start and of length len. Eg: Iota(3.0, 2) -> []float64{3.0, 4.0}
func Keys ¶
func Keys[K comparable, V any](m map[K]V) []K
Keys returns the keys of a map in the form of a slice.
func Map ¶
func Map[In, Out any](in []In, fn func(e In) Out) (out []Out)
Map executes the given function sequentially for every element on in and returns a mapped slice.
func MapParallel ¶
func MapParallel[In, Out any](in []In, fn func(e In) Out) (out []Out)
MapParallel executes the given function for every element of `in` with at most `runtime.NumCPU` goroutines. The execution order is not guaranteed, but in the end `out[ii] = fn(in[ii])` for every element.
func MultidimensionalSliceWithValue ¶
MultidimensionalSliceWithValue creates and initializes a multidimensional slice with the given value repeated everywhere. It can be cast to the appropriate type. Example:
MultidimensionalSliceWithValue(0, 3, 3) -> [][]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
All the data is allocated in one giant slice, and then partitioned accordingly.
func MustSlicesInRelData ¶
MustSlicesInRelData checks whether multidimensional slices s0 and s1 have the same shape and types, and that each of their values is within the given delta. It works with any float32 and float64 types.
A value pair from s0 and s1 is considered to be within the delta if abs(v0 - v1) / max(abs(v0), abs(v1)) <= relDelta.
An error is returned if values don't match, indicating the index of the error.
func Pop ¶
func Pop[T any](slice []T) (T, []T)
Pop the last element of the slice and returns slice with one less element. If the slice is empty, it returns the zero value for `T` and returns the slice unchanged.
func SetAt ¶
SetAt sets an element at the given `index`, where `index` can be negative, in which case it takes from the end of the slice.
func Slice2DWithValue ¶
Slice2DWithValue creates a 2D-slice of given dimensions filled with the given value.
func Slice3DWithValue ¶
Slice3DWithValue creates a 3D-slice of given dimensions filled with the given value.
func SliceToGoStr ¶
SliceToGoStr converts the slice to text, in a Go-syntax style that can be copy&pasted back to Go code. Similar to the "%#v" formatting option, but up to date for not repeating the inner-dimension slice types.
func SliceWithValue ¶
SliceWithValue creates a slice of given size filled with given value.
func SlicesInDelta ¶
SlicesInDelta checks whether multidimensional slices s0 and s1 have the same shape and types, and that each of their values is within the given delta. Works with any numeric types.
If delta <= 0, it checks for equality.
func SortedKeys ¶
SortedKeys returns the sorted keys of a map in the form of a slice.
Types ¶
This section is empty.