Documentation ¶
Overview ¶
Package sliceutil provides utilities for slicing operations and analyses in Go.
Index ¶
- func CropElement[S ~[]C, C comparable](slice S, element C) S
- func CropElements[S ~[]C, C comparable](slice S, elements ...C) S
- func CropIndex[S ~[]E, E any](slice S, index int) S
- func CropIndices[S ~[]E, E any](slice S, indices ...int) S
- func DeleteFuncs[E ~[]T, T any](slice E, funcs ...func(T) bool) E
- func IndexFuncs[E ~[]T, T any](slice E, funcs ...func(T) bool) []int
- func Interleave[E ~[]T, T any](slice E, element T, step int) E
- func PowerSet[TS ~[]T, T any](src TS) []TS
- func Prefixes[TS ~[]T, T any](slice TS) []TS
- func Prepend[E ~[]T, T any](slice E, elements ...T) E
- func Product[TS ~[]T, T any](slices ...TS) []TS
- func SelectFunc[E1 ~[]T1, E2 []T2, T1, T2 any](slice E1, selector func(T1) T2) E2
- func SelectNonZeroFunc[E1 ~[]T1, E2 []T2, T1, T2 any](slice E1, selector func(T1) T2) E2
- func Slice[TS ~[]T, T any](slice TS, from, to int) TS
- func SliceCropIndex[S ~[]E, E any](slice S, i int) S
- func Splice[TS ~[]T, T any](slice TS, from, to int, splice TS) TS
- func Suffixes[TS ~[]T, T any](slice TS) []TS
- func TakeFunc[E ~[]T, T any](slice E, pred func(T) bool) E
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CropElement ¶
func CropElement[S ~[]C, C comparable](slice S, element C) S
CropElement removes all occurrences of a specified element from a slice and returns the modified slice. It searches for the element using linear search and removes each occurrence found. This function clones the slice before modification and clips the result to ensure integrity.
func CropElements ¶
func CropElements[S ~[]C, C comparable](slice S, elements ...C) S
CropElements removes all occurrences of any of the specified elements from a slice and returns the modified slice. Each element in the input slice is checked against the specified elements, and matches are removed. The function clones the original slice before modification and ensures the result is clipped.
func CropIndex ¶
CropIndex removes an element from a slice at a specified index and returns the modified slice. It ensures the slice is clipped to remove any potential nil or zero-value elements at the end. This function clones the original slice before modification, preserving the input slice's integrity.
func CropIndices ¶
CropIndices removes elements from a slice at specified indices and returns the modified slice. Indices are processed in reverse order to maintain correct indexing after each removal. Like CropIndex, it clones the original slice before modification and clips the result.
func DeleteFuncs ¶
DeleteFuncs removes any elements from s for which any of the funcs returns true, returning the modified slice. DeleteFuncs zeroes the elements between the new length and the original length.
func IndexFuncs ¶
IndexFuncs returns the first index i satisfying f(s[i]) for any f in funcs, or nil if none do.
func Interleave ¶
Interleave inserts the element into the slice every step elements.
func PowerSet ¶
func PowerSet[TS ~[]T, T any](src TS) []TS
PowerSet returns a power-set of the input in 'src'.
Example ¶
package main import ( "fmt" "github.com/toolvox/utilgo/pkg/sliceutil" ) func main() { fmt.Println("PowerSet with []int{1, 2, 3}:") for _, set := range sliceutil.PowerSet([]int{1, 2, 3}) { fmt.Println(set) } }
Output: PowerSet with []int{1, 2, 3}: [] [1] [2] [1 2] [3] [1 3] [2 3] [1 2 3]
func Prefixes ¶
func Prefixes[TS ~[]T, T any](slice TS) []TS
Prefixes generates all prefixes of the given slice.
func Prepend ¶
func Prepend[E ~[]T, T any](slice E, elements ...T) E
Prepend inserts the items before the slice.
func Product ¶
func Product[TS ~[]T, T any](slices ...TS) []TS
Product generates the Cartesian product of variable number of slices.
Example ¶
package main import ( "fmt" "github.com/toolvox/utilgo/pkg/sliceutil" ) func main() { fmt.Println("Product of [][]int{{1, 2}, {3, 4}, {5, 6, 7}}:") for _, prod := range sliceutil.Product([][]int{{1, 2}, {3, 4}, {5, 6, 7}}...) { fmt.Println(prod) } }
Output: Product of [][]int{{1, 2}, {3, 4}, {5, 6, 7}}: [1 3 5] [1 3 6] [1 3 7] [1 4 5] [1 4 6] [1 4 7] [2 3 5] [2 3 6] [2 3 7] [2 4 5] [2 4 6] [2 4 7]
func SelectFunc ¶
func SelectFunc[E1 ~[]T1, E2 []T2, T1, T2 any](slice E1, selector func(T1) T2) E2
SelectFunc transforms each element of the input slice using the provided selector function and returns a new slice of the transformed elements.
E1 is the element type of the input slice and should be a slice type.
E2 is the element type of the resulting slice and should be a slice type.
T1 is the type of elements in the input slice.
T2 is the type of elements in the resulting slice.
func SelectNonZeroFunc ¶
func SelectNonZeroFunc[E1 ~[]T1, E2 []T2, T1, T2 any](slice E1, selector func(T1) T2) E2
SelectNonZeroFunc transforms each element of the input slice using the provided selector function, then filters out any zero values from the resulting slice. It leverages the Select function to perform the transformation and then uses pkg/slices.DeleteFunc from the slices package to remove zero values, with zero-ness determined by the pkg/github.com/toolvox/utilgo/pkg/reflectutil.IsZero function.
func SliceCropIndex ¶
SliceCropIndex removes an element from a slice at a specified index and returns the modified slice. If the index is out of bounds, the original slice is returned unmodified. Original slice may be modified by this operation.
func Splice ¶
Splice inserts the string `splice` into `s` between the specified `from` and `to` indices.
Types ¶
This section is empty.