slices

package
v0.0.0-...-02e3eee Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: GPL-3.0, LGPL-3.0 Imports: 7 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 AllEqual

func AllEqual[S ~[]E, E comparable](value E, input S) bool

AllEqual checks if all values in a slice are equal to the specified value.

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 AllSame

func AllSame[S ~[]E, E comparable](input S) bool

AllSame checks if all values in a slice are the same.

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), resolve func(K, V, V) V) map[K]V

ConvertToMap transforms a slice with data into a map. It assumes that there

  • `transform` (mandatory), executes on each slice entry to determine key and value, used for conversion to a map.
  • `resolve` (optional), resolves conflicts when multiple entries arrive at the same key in the output map. If `resolve` is nil, and a duplicate key is detected, conversion will panic.

func ConvertToMapKeys

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

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.

Note: duplicates are not resolved. Duplicates will naturally only be represented once as keys in the map. The second return value indicates number of duplicates. This function does not provide "conflict-resolution" for duplicates. (`ConvertToMap` provides more control over the conversion process.)

Returns resulting map, count of duplicate values (present only once as map keys)

func Create2D

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

Create2D creates a fully-allocated 2D dynamic array. FIXME function as bug (not applying initial value), but not always needed. Make this two separate functions?

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 Equal

func Equal[E comparable](s1, s2 []E) bool

Equal tests the contents of two slices by pair-wise testing for equality for cases where values are comparable. FIXME needs testing

func EqualFunc

func EqualFunc[E any](s1, s2 []E, test func(e1, e2 E) bool) bool

EqualFunc tests equality of slices by pair-wise testing equality of elements based on `test` function. FIXME needs testing

func EqualT

func EqualT[E builtin.Equaler[E]](s1, s2 []E) bool

EqualT tests equality of slices by pair-wise testing equality of elements based on builtin.Equaler. FIXME needs testing

func Extend

func Extend[E any](slice []E, additions ...E) ([]E, error)

Extend appends to a slice within the available capacity of the slice, without reallocation.

func ExtendFrom

func ExtendFrom[E any](slice []E, additions []E) ([]E, error)

ExtendFrom appends to a slice as far as capacity allows, without reallocation.

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 LastIndex

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

LastIndex looks for an element linearly from the end and returns the index if found, which would be the last occurrence of specified value, or `-1` if not found.

func LastIndexFunc

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

LastIndexFunc tests elements linearly from the end and returns the index of the first element that tests positively, which would be the last occurrence of a value that satisfies `test`, or `-1` if not 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 Reverse

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

Reverse a slice in-place. (Calls into Go std slices.Reverse)

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