Documentation ¶
Index ¶
- func All[E any](input []E, test func(E) bool) bool
- func AllIndexed[E any](input []E, test func(int, E) bool) bool
- func Any[E any](input []E, test func(E) bool) bool
- func AnyIndexed[E any](input []E, test func(int, E) bool) bool
- func AppendCond[E any](cond bool, slice []E, value ...E) []E
- func ContainedIn[E comparable](data []E) func(element E) bool
- func Contains[E comparable](slice []E, value E) bool
- func ContainsFunc[E any](slice []E, test func(E) bool) bool
- func ConvertToMap[E any, K comparable, V any](input []E, transform func(int, E) (K, V)) map[K]V
- func ConvertToMapKeys[K comparable, V any](input []K, transform func(int, K) V) map[K]V
- func Create2D[E any](sizeX, sizeY uint, initial E) [][]E
- func DistinctElementCount[E comparable](data []E) map[E]uint
- func DistinctElements[E comparable](data []E) map[E]struct{}
- func Duplicate[T any](src []T) []T
- func Empty2DFrom[E, V any](prototype [][]E, initial V) [][]V
- func Empty3DFrom[E, V any](prototype [][][]E, initial V) [][][]V
- func EmptyFrom[E, V any](prototype []E, initial V) []V
- func Filter[E any](input []E, filter func(E) bool) []E
- func FilterIndexed[E any](input []E, filter func(int, E) bool) []E
- func Fold[E any, V any](input []E, initial V, fold func(V, E) V) V
- func FoldIndexed[E any, V any](input []E, initial V, fold func(V, int, E) V) V
- func ForEach[E any](data []E, process func(e E))
- func ForEachIndexed[E any](data []E, process func(idx int, e E))
- func Index[E comparable](data []E, value E) int
- func IndexFunc[E any](data []E, test func(E) bool) int
- func MiddleElement[E any](slice []E) (int, E, error)
- func MiddleIndex[E any](slice []E) (int, error)
- func MoveElementN[E any](input []E, idx int, n int)
- func MoveElementTo[E any](input []E, from, to int)
- func NotContainedIn[E comparable](data []E) func(e E) bool
- func Reduce[E any](input []E, reduce func(e1, e2 E) E) E
- func Reversed[E any](slice []E) []E
- func Transform[I, O any](input []I, transform func(I) O) []O
- func TransformIndexed[I, O any](input []I, transform func(int, I) O) []O
- func UniformDimensions2D[E any](slice [][]E) bool
- func Update[E any](input []E, update func(E) E)
- func UpdateIndexed[E any](input []E, update func(int, E) E)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllIndexed ¶
All checks if all values in a slice satisfy `test`, that is `test` returns true.
func Any ¶
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 ¶
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 ¶
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 ¶
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 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
TODO consider defining `ErrInvalid` error independent of `os` package
func MiddleIndex ¶
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 ¶
MoveElementN moves an element any number of positions in a slice by repeated performing in-place swaps of elements.
func MoveElementTo ¶
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 ¶
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 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 ¶
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.