Documentation ¶
Overview ¶
Package slices offers various utilities for handling and manipulating slice data structures.
Index ¶
- func Chunk[T any](slice []T, n int) (cs [][]T)
- func Compact[T any](s []*T) (ret []*T)
- func Contains[T comparable](s []T, c T) bool
- func ContainsHashable[H godash.Hashable](s []H, c H) bool
- func ContainsWith[S, C any](s []S, c C, pred func(S, C) bool) bool
- func Difference[T comparable](a, b []T) (ret []T)
- func DifferenceHashable[H godash.Hashable](a, b []H) []H
- func DifferenceWith[T any](a, b []T, p func(T, T) bool) (ret []T)
- func Filter[T any](s []T, p func(T) bool) (res []T)
- func Flatten[T any](a [][]T) (b []T)
- func Foreach[T any](s []T, f func(T))
- func Head[T any](s []T) T
- func Intersection[T comparable](a []T, b []T) (c []T)
- func IntersectionHashable[T godash.Hashable](a []T, b []T) (c []T)
- func IntersectionWith[T any](a []T, b []T, p func(T, T) bool) (c []T)
- func Map[T, U any](s []T, f func(T) U) (res []U)
- func Reduce[T, U any](s []T, f func(U, T) U, init U) (res U)
- func Sample[T any](s []T) T
- func SampleN[T any](s []T, n int) []T
- func SampleNWithSeed[T any](s []T, n int, seed int64) []T
- func SampleWithSeed[T any](s []T, seed int64) T
- func Shuffle[T any](s []T) []T
- func ShuffleWithSeed[T any](s []T, seed int64) []T
- func Tail[T any](s []T) []T
- func Union[T comparable](a []T, b []T) (c []T)
- func UnionHashable[T godash.Hashable](a []T, b []T) (c []T)
- func UnionWith[T any](a []T, b []T, p func(T, T) bool) (c []T)
- func Unzip[T, U any](pairs []godash.Pair[T, U]) (a []T, b []U)
- func Zip[T, U any](a []T, b []U) (pairs []godash.Pair[T, U])
- func ZipWithIndex[T any](s []T) (pairs []godash.Pair[int, T])
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶
Chunk returns a slice of slices of size n. If the slice cannot be evenly divided into chunks of size n, the last chunk will be smaller than size n.
func Compact ¶
func Compact[T any](s []*T) (ret []*T)
Compact removes all nil values from the slice.
func Contains ¶
func Contains[T comparable](s []T, c T) bool
Contains returns true if the slice contains the value.
func ContainsHashable ¶
ContainsHashable returns true if the slice contains the value using the provided hash function.
func ContainsWith ¶
ContainsWith returns true if the slice contains the value using the provided predicate.
func Difference ¶
func Difference[T comparable](a, b []T) (ret []T)
Difference returns the difference between two slices.
func DifferenceHashable ¶
DifferenceHashable returns the difference between two slices using the provided hash function.
func DifferenceWith ¶
DifferenceWith returns the difference between two slices using the provided predicate.
func Filter ¶
Filter returns a new slice containing only the elements of the given slice that satisfy the given predicate.
func Flatten ¶
func Flatten[T any](a [][]T) (b []T)
Flatten return a slice after flattening the input slice by one level.
func Foreach ¶
func Foreach[T any](s []T, f func(T))
Foreach iterates over the given slice and calls the given function for each element. Should be used for side effects only since it doesn't return a value.
func Head ¶
func Head[T any](s []T) T
Head returns the first element in a slice, this is the same as the 0th element.
func Intersection ¶
func Intersection[T comparable](a []T, b []T) (c []T)
Intersection return the intersection of the two input slices. Standard union semantics apply. This is a fast implementation because it allows only comparable types.
func IntersectionHashable ¶
IntersectionHashable returns the intersection of the two input slices. Standard union semantics apply. This is a slow implementation, but it allows for custom equality semantics.
func IntersectionWith ¶
IntersectionWith returns the intersection of the two input slices. Standard union semantics apply. This is a slow implementation, but it allows for custom equality semantics. The predicate p is used to determine whether two elements are equal.
func Map ¶
func Map[T, U any](s []T, f func(T) U) (res []U)
Map applies the function f to each element of the slice and returns a new slice with the results.
func Reduce ¶
func Reduce[T, U any](s []T, f func(U, T) U, init U) (res U)
Reduce applies the function f to each element of the slice and returns a single value.
func Sample ¶ added in v0.11.0
func Sample[T any](s []T) T
Sample returns a random element from the slice.
func SampleNWithSeed ¶ added in v0.11.0
SampleNWithSeed returns n random elements from the slice with the provided seed.
func SampleWithSeed ¶ added in v0.11.0
SampleWithSeed returns a random element from the slice with the provided seed.
func ShuffleWithSeed ¶ added in v0.11.0
ShuffleWithSeed shuffles the slice with the provided seed in place.
func Tail ¶
func Tail[T any](s []T) []T
Tail returns all elements of the slice except the head. This is the same as the slice from index 1 to the end.
func Union ¶
func Union[T comparable](a []T, b []T) (c []T)
Union returns a slice containing the elements of the two input slices. Standard union semantics apply. This is a fast implementation because it allows only comparable types.
func UnionHashable ¶
UnionHashable returns a slice containing the elements of the two input slices. Standard union semantics apply. This is a fast implementation because it allows only hashable types.
func UnionWith ¶
UnionWith returns a slice containing the elements of the two input slices. Standard union semantics apply. This is a slow implementation, but it allows for custom equality semantics. The predicate p is used to determine whether two elements are equal.
func Unzip ¶
Unzip returns two slices, the first containing the first elements of the input pairs, and the second containing the second elements of the input pairs.
Types ¶
This section is empty.