slices

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 20, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package slices are

Index

Constants

View Source
const Epsilon = 1e-4

Variables

This section is empty.

Functions

func Close

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

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

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 IotaSlice

func IotaSlice(start, len int) (slice []int)

IotaSlice returns a slice of incremental int values, starting with start and of length len. Eg: IotaSlice(3, 2) -> []int{3, 4}

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

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 ReverseSlice

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

ReverseSlice returns the reverse 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