Documentation ¶
Overview ¶
Package slice contains generic utility functions on slices
Package slice contains generic utility functions on slices
Index ¶
- func Contains[T comparable](l []T, v T) bool
- func Deduplicate[T comparable](in []T) []T
- func DumbSort[T constraints.Ordered](ts []T) []T
- func Filter[T any](in []T, f func(T) bool) (out []T)
- func Fold[A, B any](as []A, f func(A, B) B, acc B) B
- func Index[T comparable](ts []T, v T) int
- func Intersect[T comparable](as []T, bs []T) (ts []T)
- func Map[A, B any](as []A, f func(A) B) []B
- func MapFilter[A, B any](slice []A, f func(A) (B, bool)) (out []B)
- func Reduce[A, B any](as []A, f func(A, B) B, acc B) B
- func Remove[T comparable](xs []T, d T) []T
- func RemoveAll[T comparable](xs []T, v T) []T
- func Reverse[T any](a []T) []T
- func Shuffle[T any](l []T) []T
- func Sort[T constraints.Ordered](ts []T) []T
- func ToMap[K comparable, V, A any](as []A, f func(A) (K, V)) map[K]V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](l []T, v T) bool
Contains checks for the existence of an element in the slice
func Fold ¶
func Fold[A, B any](as []A, f func(A, B) B, acc B) B
Fold folds the slice into a single value using the given function and accumulator
func Index ¶
func Index[T comparable](ts []T, v T) int
Index returns the index of a value if it exists it will return -1 if it does not exist in the slice
func Intersect ¶
func Intersect[T comparable](as []T, bs []T) (ts []T)
Intersect returns the intersection of two slices
func Map ¶
func Map[A, B any](as []A, f func(A) B) []B
Map create a new slice from an existing one using a map function
func MapFilter ¶
MapFilter combines Map and Filter into one process It takes a function that takes a value of type A and returns a value of type B, and a bool for whether or not to keep the value.
func Reduce ¶
func Reduce[A, B any](as []A, f func(A, B) B, acc B) B
Reduce reduces the slice to a single value using the given fucntion and accumulator
func Remove ¶
func Remove[T comparable](xs []T, d T) []T
Remove will remove from the slice the first match of the given value
func RemoveAll ¶
func RemoveAll[T comparable](xs []T, v T) []T
RemoveAll removes all occurances of the given value in the slice
func Shuffle ¶
func Shuffle[T any](l []T) []T
Shuffle shuffles a slice using the Fisher-Yates algorithm you need to call `rand.Seed` for this to work properly
func ToMap ¶
func ToMap[K comparable, V, A any](as []A, f func(A) (K, V)) map[K]V
ToMap converts a slice to a map using the given function which takes an element and returns a key and value
Types ¶
This section is empty.