Documentation ¶
Overview ¶
Package slices are
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 Different[T comparable](e0, e1 any) bool
- func Equal[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 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 constraints.Ordered](slice []T) (max T)
- func Min[T constraints.Ordered](slice []T) (min T)
- func MultidimensionalSliceWithValue[T any](value T, dims ...int) any
- func Pop[T any](slice []T) (T, []T)
- func ReverseSlice[T any](slice []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 constraints.Ordered, V any](m map[K]V) []K
Constants ¶
const Epsilon = 1e-4
Variables ¶
This section is empty.
Functions ¶
func At ¶ added in v0.4.0
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 ¶ added in v0.4.0
func Copy[T any](slice []T) []T
Copy creates a new (shallow) copy of T. A short cut 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 Different ¶
func Different[T comparable](e0, e1 any) bool
Different is a comparison function that tests that values are different, and can be fed to DeepSliceCmp.
func Equal ¶
func Equal[T comparable](e0, e1 any) bool
Equal 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 slice is not a slice or if value is not the base type of slice.
func FillSlice ¶
func FillSlice[T any](slice []T, value T)
FillSlice with fill the slice with the given value.
func Flag ¶ added in v0.4.0
func Flag[T any](name string, defaultValue []T, usage string, parserFn func(valueStr string) (T, error)) *[]T
Flag creates a flag with the given name, description and default value.
func Iota ¶ added in v0.4.1
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 ¶ added in v0.2.0
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 ¶ added in v0.2.0
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 Max ¶ added in v0.2.1
func Max[T constraints.Ordered](slice []T) (max T)
Max scans the slice and returns the maximum value.
func Min ¶ added in v0.2.1
func Min[T constraints.Ordered](slice []T) (min T)
Min scans the slice and returns the smallest value.
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 Pop ¶ added in v0.4.0
func Pop[T any](slice []T) (T, []T)
Pop last element of the slice, and returns slice with one less element. If slice is empty it returns the zero value for `T` and returns slice unchanged.
func ReverseSlice ¶
func ReverseSlice[T any](slice []T)
ReverseSlice returns the reverse of a slice.
func SetAt ¶ added in v0.4.0
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 SetLast ¶ added in v0.4.0
func SetLast[T any](slice []T, value T)
SetLast sets the last element of a 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 %#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 are within the given delta. Works with any numeric types.
If delta <= 0, it checks for equality.
func SortedKeys ¶
func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K
SortedKeys returns the sorted keys of a map in the form of a slice.
Types ¶
This section is empty.