Documentation
¶
Overview ¶
Package cuts provides useful, generic utilities for working with slices.
Index ¶
- func AllWhere[S ~[]E, E any](vals S, where func(val E) bool) bool
- func AnyWhere[S ~[]E, E any](vals S, where func(val E) bool) bool
- func ChunkBy[T any](items []T, chunkSize int) (chunks [][]T)
- func Dedupe[T comparable](in []T) []T
- func DedupeFunc[T any, E comparable](in []T, cmp func(t T) E) []T
- func FirstWhere[S ~[]E, E any](vals S, where func(val E) bool) (int, bool)
- func LastWhere[S ~[]E, E any](vals S, where func(val E) bool) (int, bool)
- func SnapTo[S ~[]E, E constraints.Integer | constraints.Float](vals S, target E) E
- func SnapToFunc[S ~[]E, E any](vals S, target E, cmp func(E, E) int, closest func(tgt, nxt, prv E) E) E
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dedupe ¶
func Dedupe[T comparable](in []T) []T
Dedupe removes duplicate values from an array of comparable elements. It preserves the order of the original array.
func DedupeFunc ¶ added in v0.0.2
func DedupeFunc[T any, E comparable](in []T, cmp func(t T) E) []T
DedupeFunc removes duplicate values from an array of elements of type T, from which a comparable type E can be derived using the provided function. It does not preserve the order of the original array.
func FirstWhere ¶
FirstWhere searches for the first element in a sorted slice that meets the condition function and returns its position. If no such element exists, returns -1; it also returns a bool saying whether an element matching the condition was found in the slice. The slice must be sorted in increasing order.
func LastWhere ¶
LastWhere searches for the last element in a sorted slice that meets the condition function and returns its position. If no such element exists, returns -1; it also returns a bool saying whether an element matching the condition was found in the slice. The slice must be sorted in increasing order.
func SnapTo ¶
func SnapTo[S ~[]E, E constraints.Integer | constraints.Float](vals S, target E) E
SnapTo returns the element in an array that is closest to the target.
func SnapToFunc ¶
func SnapToFunc[S ~[]E, E any](vals S, target E, cmp func(E, E) int, closest func(tgt, nxt, prv E) E) E
SnapToFunc returns the element in an array that is closest to the target, given a custom comparison and closest function.
Types ¶
This section is empty.