Documentation
¶
Overview ¶
Package sliceutils provides functional utilities for working with slices in Go, providing a rich set of operations for transforming, filtering, and manipulating slice data.
See README.md for details.
Index ¶
- Variables
- func All[S ~[]T, T any](xs S, predicate func(T) bool) bool
- func Any[S ~[]T, T any](xs S, predicate func(T) bool) bool
- func Combinations[S ~[]T, T any](s S, k uint) iter.Seq[S]
- func ContainsFunc[S ~[]T, T any](xs S, x T, equal func(T, T) bool) bool
- func Count[S ~[]T, T any](xs S, predicate func(T) bool) int
- func CountUnique[S ~[]T, T comparable](xs S) int
- func CountUniqueFunc[S ~[]T, T any](xs S, equal func(T, T) bool) int
- func Fill[T any](s []T, x T)
- func Filter[S ~[]T, T any](xs S, predicate func(T) bool) S
- func Fold[T, U any](f func(acc U, x T) U, initial U, rest ...T) U
- func FoldOrError[T, U any](f func(acc U, x T) (U, error), initial U, rest ...T) (U, error)
- func IsAllUnique[S ~[]T, T comparable](xs S) bool
- func IsAllUniqueFunc[S ~[]T, T any](xs S, equal func(T, T) bool) bool
- func IsSubSet[SB, SP ~[]T, T comparable](sub SB, sup SP) bool
- func IsSubSetFunc[SB, SP ~[]T, T any](sub SB, sup SP, equal func(T, T) bool) bool
- func IsSuperSet[T comparable](ss, s []T) bool
- func KCoveringCombinations[S ~[]T, T any](s S, k uint) iter.Seq[S]
- func Map[TOut any, SIn ~[]TIn, TIn any](in SIn, f func(TIn) TOut) []TOut
- func MapCast[S ~[]TOut, TOut any, SIn ~[]TIn, TIn any](in SIn, f func(TIn) TOut) S
- func MapOrError[SIn ~[]TIn, TIn, TOut any](in SIn, f func(TIn) (TOut, error)) (out []TOut, err error)
- func PadToLeft[S ~[]T, T any](xs S, padLength int) S
- func PadToLeftWith[S ~[]T, T any](xs S, padLength int, pad T) S
- func PadToRight[S ~[]T, T any](xs S, padLength int) S
- func PadToRightWith[S ~[]T, T any](xs S, padLength int, pad T) S
- func Reduce[S ~[]T, T any](xs S, initial T, f func(T, T) T) T
- func Repeat[S ~[]T, T any](x T, n int) S
- func Reverse[S ~[]T, T any](xs S) S
- func Reversed[S ~[]T, T any](xs S) S
- func Shuffle[S ~[]T, T any](xs S, prng io.Reader) (S, error)
- func Shuffled[S ~[]T, T any](xs S, prng io.Reader) (S, error)
Constants ¶
This section is empty.
Variables ¶
var ErrArgumentIsNil = errs.New("argument is nil")
Functions ¶
func Combinations ¶
Combinations generates all k-combinations of the input slice s.
func ContainsFunc ¶
ContainsFunc returns true if xs contains the element x using the provided equality function.
func CountUnique ¶
func CountUnique[S ~[]T, T comparable](xs S) int
CountUnique returns the number of unique elements in xs.
func CountUniqueFunc ¶
CountUniqueFunc returns the number of unique elements in xs using the provided equality function.
func Filter ¶
Filter returns a new slice containing only the elements of xs that satisfy the predicate.
func Fold ¶
func Fold[T, U any](f func(acc U, x T) U, initial U, rest ...T) U
Fold reduces the slice rest to a single value by applying the binary function f cumulatively, starting with initial.
func FoldOrError ¶
FoldOrError reduces the slice rest to a single value by applying the binary function f cumulatively, starting with initial.
func IsAllUnique ¶
func IsAllUnique[S ~[]T, T comparable](xs S) bool
IsAllUnique returns true if all elements in xs are unique.
func IsAllUniqueFunc ¶
IsAllUniqueFunc returns true if all elements in xs are unique using the provided equality function.
func IsSubSet ¶
func IsSubSet[SB, SP ~[]T, T comparable](sub SB, sup SP) bool
IsSubSet returns true if all elements of sub are present in sup.
func IsSubSetFunc ¶
IsSubSetFunc returns true if all elements of sub are present in sup using the provided equality function.
func IsSuperSet ¶
func IsSuperSet[T comparable](ss, s []T) bool
IsSuperSet returns true if ss contains all elements of s.
func KCoveringCombinations ¶
KCoveringCombinations generates all combinations of the input slice s with sizes from k to len(s).
func Map ¶
Map applies the function f to each element of the input slice in, returning a slice of the output type.
func MapCast ¶
MapCast applies the function f to each element of the input slice in, casting the output slice to the desired type S.
func MapOrError ¶
func MapOrError[SIn ~[]TIn, TIn, TOut any](in SIn, f func(TIn) (TOut, error)) (out []TOut, err error)
MapOrError applies the function f to each element of the input slice in,.
func PadToLeft ¶
PadToLeft pads the input slice xs to the left with zero values to reach the desired padLength.
func PadToLeftWith ¶
PadToLeftWith pads the input slice xs to the left with the specified pad value to reach the desired padLength.
func PadToRight ¶
PadToRight pads the input slice xs to the right with zero values to reach the desired padLength.
func PadToRightWith ¶
PadToRightWith pads the input slice xs to the right with the specified pad value to reach the desired padLength.
func Reduce ¶
func Reduce[S ~[]T, T any](xs S, initial T, f func(T, T) T) T
Reduce reduces the slice xs to a single value by applying the binary function f cumulatively.
func Reverse ¶
func Reverse[S ~[]T, T any](xs S) S
Reverse reverses the input slice in place and returns it.
func Reversed ¶
func Reversed[S ~[]T, T any](xs S) S
Reversed returns a new slice that is the reverse of the input slice.
func Shuffle ¶
Shuffle uses Fisher-Yates algorithm to produce a random permutation of the input. https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithmreally It ought not be called with relatively big len(xs) (e.g. that doesn't fit in 32 bits). Not only will it take a very long time, but with 2³¹! possible permutations, there's no way that any PRNG can have a big enough internal state to generate even a minuscule percentage of the possible permutations.
Types ¶
This section is empty.