Documentation
¶
Overview ¶
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 Difference[T comparable](as []T, bs []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 Intersection[T comparable](as []T, bs []T) []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 Max[A constraints.Ordered](as []A) A
- func MaxFunc[A any](as []A, less func(A, A) bool) A
- func Min[A constraints.Ordered](as []A) A
- func MinFunc[A any](as []A, less func(A, A) bool) A
- 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 any](l []T, less func(T, T) bool) []T
- func ToMap[K comparable, V, A any](as []A, f func(A) (K, V)) map[K]V
- func Union[T comparable](as []T, bs []T) []T
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 Deduplicate ¶
func Deduplicate[T comparable](in []T) []T
Deduplicate removes duplicates in a slice
func Difference ¶ added in v1.0.0
func Difference[T comparable](as []T, bs []T) []T
Difference returns the difference of two sets (A - B)
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 Intersection ¶ added in v1.0.0
func Intersection[T comparable](as []T, bs []T) []T
Intersection returns the intersection of two slices, a slice of everything contained in both 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 Max ¶ added in v1.0.0
func Max[A constraints.Ordered](as []A) A
Max returns the maximum element in a slice
func MaxFunc ¶ added in v1.0.0
MaxFunc returns the maximum element in a slice using a given "less than" function
func Min ¶ added in v1.0.0
func Min[A constraints.Ordered](as []A) A
Min returns the minimum element in a slice
func MinFunc ¶ added in v1.0.0
MinFunc returns the minimum element in a slice using a given "less than function"
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
func Union ¶ added in v1.0.0
func Union[T comparable](as []T, bs []T) []T
Union returns the union of two slices
Duplicates in the first slice will be left. Duplicates in the second slice will be removed.
Types ¶
This section is empty.