Documentation
¶
Overview ¶
Package slices contains a utility functions to work with slices.
Index ¶
- func Clip[S ~[]E, E any](s S) S
- func Clone[S ~[]E, E any](s S) S
- func Contains[T any](s []T, fn func(T) bool) bool
- func Copy[S ~[]V, V any](s S, n int) S
- func Filter[S ~[]T, T any](slc S, fn func(T) bool) S
- func FilterInPlace[S ~[]V, V any](slc S, fn func(V) bool) S
- func FlatMap[T, R any](slc []T, fn func(T) []R) []R
- func Grow[S ~[]E, E any](s S, n int) S
- func IndexFunc[T any](slc []T, fn func(T) bool) int
- func Map[T, R any](slc []T, fn func(T) R) []R
- func ToMap[T any, K comparable, V any](slc []T, fn func(T) (K, V)) map[K]V
- func ToSet[T comparable](slc []T) map[T]struct{}
- func ToSetFunc[T any, K comparable](slc []T, fn func(T) K) map[K]struct{}
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clip ¶ added in v0.4.5
func Clip[S ~[]E, E any](s S) S
Clip removes unused capacity from the slice, returning s[:len(s):len(s)].
func Clone ¶ added in v0.4.5
func Clone[S ~[]E, E any](s S) S
Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone.
func Copy ¶
Copy copies first n elements. If n is greater than the length of the slice, it will copy the whole slice.
func FilterInPlace ¶
FilterInPlace filters the slice in place.
func FlatMap ¶
func FlatMap[T, R any](slc []T, fn func(T) []R) []R
FlatMap applies the function fn to each element of the slice and returns a new slice with the results. It flattens the result of fn into the result slice.
func Grow ¶ added in v0.4.5
Grow increases the slice's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the slice without another allocation. If n is negative or too large to allocate the memory, Grow panics.
func Map ¶
func Map[T, R any](slc []T, fn func(T) R) []R
Map applies the function fn to each element of the slice and returns a new slice with the results.
func ToMap ¶
func ToMap[T any, K comparable, V any](slc []T, fn func(T) (K, V)) map[K]V
ToMap converts a slice to a map.
func ToSetFunc ¶
func ToSetFunc[T any, K comparable](slc []T, fn func(T) K) map[K]struct{}
ToSetFunc converts a slice to a set using the function fn.
Types ¶
This section is empty.