slices

package
v0.0.0-...-34bb4ad Latest Latest
Warning

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

Go to latest
Published: May 12, 2024 License: GPL-3.0, LGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[E any](input []E, test func(E) bool) bool

All checks if all values in a slice satisfy `test`, that is `test` returns true.

func AllIndexed

func AllIndexed[E any](input []E, test func(int, E) bool) bool

All checks if all values in a slice satisfy `test`, that is `test` returns true.

func Any

func Any[E any](input []E, test func(E) bool) bool

Any iterates over elements in the slice and tests if they satisfy `test`. Result is returned upon first element found, and will iterate over all elements if none satisfy the condition.

func AnyIndexed

func AnyIndexed[E any](input []E, test func(int, E) bool) bool

AnyIndexed iterates over elements in the slice and tests if they satisfy `test`. Result is returned upon first element found, and will iterate over all elements if none satisfy the condition.

func AppendCond

func AppendCond[E any](cond bool, slice []E, value ...E) []E

AppendCond appends to a slice if condition holds true.

func ContainedIn

func ContainedIn[E comparable](data []E) func(element E) bool

func Contains

func Contains[E comparable](slice []E, value E) bool

Contains checks if provided value is present anywhere in the slice.

func ContainsFunc

func ContainsFunc[E any](slice []E, test func(E) bool) bool

ContainsFunc checks any element in the slice satisfies func `test`.

func ConvertToMap

func ConvertToMap[E any, K comparable, V any](input []E, transform func(int, E) (K, V)) map[K]V

ConvertToMap transforms a slice with data into a map. It assumes that there FIXME consider how to deal with duplicate keys. The transform function should be able to assume no overlapping values. Otherwise input should be sanitized first.

func ConvertToMapKeys

func ConvertToMapKeys[K comparable, V any](input []K, transform func(int, K) V) map[K]V

TransformSliceToMapKeys assumes non-overlapping map keys, meaning that there will be the same number of keys in the output as there are entries in the input slice. This assumption exists to be able to detect loss of information, due to faulty logic. TODO consider changing this to a "MergeSliceIntoMapKeys" that does not create the map itself and provides mutating logic. TODO consider renaming to ConvertSliceToMapKeys

func Create2D

func Create2D[E any](sizeX, sizeY uint, initial E) [][]E

Create2D creates a fully-allocated 2D dynamic array.

func DistinctElementCount

func DistinctElementCount[E comparable](data []E) map[E]uint

DistinctElementCount summarizes the contents of the slice as a multiset/bag containing each distinct element with a count for the number of occurrences. FIXME reconsider name for something shorter.

func DistinctElements

func DistinctElements[E comparable](data []E) map[E]struct{}

DistinctElements summarizes the contents of the slice as a set containing each distinct element present. FIXME reconsider name for something shorter.

func Duplicate

func Duplicate[T any](src []T) []T

Duplicate copies the provided source slice to new slice of same size. Copying is a shallow copy operation.

func Empty2DFrom

func Empty2DFrom[E, V any](prototype [][]E, initial V) [][]V

Empty2DFrom creates an empty 2D slice with dimensions from the provided prototype.

func Empty3DFrom

func Empty3DFrom[E, V any](prototype [][][]E, initial V) [][][]V

Empty3DFrom creates an empty 3D slice with dimensions from the provided prototype.

func EmptyFrom

func EmptyFrom[E, V any](prototype []E, initial V) []V

EmptyFrom creates an empty (multi-dimensional) slice with initial values `initial`.

func Filter

func Filter[E any](input []E, filter func(E) bool) []E

Filter takes a slice `input` and a function `filter`. If `filter` returns true, the value is preserved. If `filter` returns false, the value is dropped.

func FilterIndexed

func FilterIndexed[E any](input []E, filter func(int, E) bool) []E

FilterIndexed takes a slice `input` and a function `filter`. If `filter` returns true, the value is preserved. If `filter` returns false, the value is dropped.

func Fold

func Fold[E any, V any](input []E, initial V, fold func(V, E) V) V

Fold folds a slice `input` to a single aggregate value of type `V`, using `initial V` as starting value. Function `fold` defines exactly how `V` is determined from each entry.

func FoldIndexed

func FoldIndexed[E any, V any](input []E, initial V, fold func(V, int, E) V) V

FoldIndexed folds a slice `input` to a single aggregate value of type `V`, using `initial V` as starting value. Function `reduce` defines exactly how `V` is determined with each entry. FoldIndexed uses callback function `fold` that receives the slice index in addition to the value.

func ForEach

func ForEach[E any](data []E, process func(e E))

ForEach executes a closure for every value in `data`

func ForEachIndexed

func ForEachIndexed[E any](data []E, process func(idx int, e E))

ForEachIndexed executes a closure for every value in `data`

func Index

func Index[E comparable](data []E, value E) int

Index looks for a value linearly in the slice and returns its index if found, or -1 otherwise.

func IndexFunc

func IndexFunc[E any](data []E, test func(E) bool) int

IndexFunc looks for an element linearly in the slice and returns its index if found, or -1 otherwise. Func `test` is used to test if the value is found.

func MiddleElement

func MiddleElement[E any](slice []E) (int, E, error)

TODO consider defining `ErrInvalid` error independent of `os` package

func MiddleIndex

func MiddleIndex[E any](slice []E) (int, error)

MiddleIndex looks up the middle position in an odd-sized slice and returns its index. If even-sized, it returns `0` and an `os.ErrInvalid` with context. TODO this may be too much: consider moving to different `slice` package or something, akin to `set` or `multiset` I'd guess.

func MoveElementN

func MoveElementN[E any](input []E, idx int, n int)

MoveElementN moves an element any number of positions in a slice by repeated performing in-place swaps of elements.

func MoveElementTo

func MoveElementTo[E any](input []E, from, to int)

MoveElementTo moves an element at given index `from` in `input` to index `to`.

func NotContainedIn

func NotContainedIn[E comparable](data []E) func(e E) bool

func Reduce

func Reduce[E any](input []E, reduce func(e1, e2 E) E) E

Reduce reduces a slice `input` to a single element (of same type), using function `reduce`. The first input element is assumed as the initial reduction result. `reduce` is applied to the result and next input element, until the last element is processed. The reduction result is then returned.

A singleton-slice will have its first/only element as a result.

(See `Fold` for folding/"reducing" to another data-type or with special initial values.)

func Reversed

func Reversed[E any](slice []E) []E

Reversed creates a new slice with the contents of provided slice in reversed order.

func Transform

func Transform[I, O any](input []I, transform func(I) O) []O

Transform maps a slice of data-type `I` to a function `I -> O` and returns a result slice of data-type `O`. The result slice is immediately allocated with equal capacity to minimize allocations.

func TransformIndexed

func TransformIndexed[I, O any](input []I, transform func(int, I) O) []O

TransformIndexed maps a slice of data-type `I` to a function `idx, I -> O` and returns a result slice of data-type `O`.` The result slice is immediately allocated with equal capacity to minimize allocations.

func UniformDimensions2D

func UniformDimensions2D[E any](slice [][]E) bool

func Update

func Update[E any](input []E, update func(E) E)

Update updates all elements of a slice using the provided `update` func. Elements are passed in in isolation, therefore the update logic must operate on individual elements.

func UpdateIndexed

func UpdateIndexed[E any](input []E, update func(int, E) E)

UpdateIndexed updates all elements of a slice using the provided `update` func. Elements are passed in in isolation, therefore the update logic must operate on individual elements.

Types

This section is empty.

Jump to

Keyboard shortcuts

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