slices

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package slices are

Index

Constants

View Source
const Epsilon = 1e-4

Variables

This section is empty.

Functions

func At added in v0.4.0

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

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 Close

func Close[T interface{ float32 | float64 }](e0, e1 any) bool

Close is a comparison function that can be fed to DeepSliceCmp.

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

func DeepSliceCmp(s0, s1 any, cmpFn func(e0, e1 any) bool) bool

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

func FillAnySlice(slice any, value any)

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 Last added in v0.4.0

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

Last returns the last element 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

func MultidimensionalSliceWithValue[T any](value T, dims ...int) any

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

func SetAt[T any](slice []T, index int, value T)

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

func Slice2DWithValue[T any](value T, dim0, dim1 int) [][]T

Slice2DWithValue creates a 2D-slice of given dimensions filled with the given value.

func Slice3DWithValue

func Slice3DWithValue[T any](value T, dim0, dim1, dim2 int) [][][]T

Slice3DWithValue creates a 3D-slice of given dimensions filled with the given value.

func SliceToGoStr

func SliceToGoStr(slice any) string

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

func SliceWithValue[T any](size int, value T) []T

SliceWithValue creates a slice of given size filled with given value.

func SlicesInDelta

func SlicesInDelta(s0, s1 any, delta float64) bool

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.

Jump to

Keyboard shortcuts

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