sliceutils

package
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 3 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk added in v1.2.2

func Chunk[T any](input []T, size int) [][]T

Chunk - receives a slice of type T and size N and returns a slice of slices T of size N.

func Compact added in v1.9.0

func Compact[T comparable](slice []T) []T

Compact removes all zero values from the slice. A zero value is determined by the zero value of type T.

func Copy deprecated

func Copy[T any](slice []T) []T

Copy - receives a slice of type T and copies it.

Deprecated: Use slices.Clone from the standard library instead:

cloned := slices.Clone(slice)

func CountBy added in v1.9.0

func CountBy[T any, K comparable](slice []T, keySelector func(T) K) map[K]int

CountBy counts the occurrences of each key extracted using the keySelector function. Returns a map where keys are the extracted keys and values are the counts.

func Difference

func Difference[T comparable](slices ...[]T) []T

Difference - takes a variadic number of slices of type T and returns a slice of type T containing the elements that are different between the slices. For example, given []int{1, 2, 3}, []int{2, 3, 4}, []{3, 4, 5}, the difference would be []int{1, 5}.

func DistinctBy added in v1.9.0

func DistinctBy[T any, K comparable](slice []T, keySelector func(T) K) []T

DistinctBy returns a slice containing only the first occurrence of each element, where uniqueness is determined by the key returned from the keySelector function.

func EnsureUniqueAndAppend added in v1.8.0

func EnsureUniqueAndAppend[T comparable](slice []T, item T) []T

EnsureUniqueAndAppend - Appends an item to a slice if it does not already exist.

func Every

func Every[T any](slice []T, predicate func(value T, index int, slice []T) bool) bool

Every - given a slice of type T, executes the given predicate for each element of the slice. If the predicate returns true for all elements, it returns true, otherwise it returns false. The function is passed the current element, the current index and the slice itself as function arguments.

func Filter

func Filter[T any](slice []T, predicate func(value T, index int, slice []T) bool) (filtered []T)

Filter - given a slice of type T, executes the given predicate function on each element in the slice. The predicate is passed the current element, the current index and the slice itself as function arguments. If the predicate returns true, the value is included in the result, otherwise it is filtered out.

func Find

func Find[T any](slice []T, predicate func(value T, index int, slice []T) bool) *T

Find - given a slice of type T, executes the passed in predicate function for each element in the slice. If the predicate returns true - a pointer to the element is returned. If no element is found, nil is returned. The function is passed the current element, the current index and the slice itself as function arguments.

func FindIndex

func FindIndex[T any](slice []T, predicate func(value T, index int, slice []T) bool) int

FindIndex - given a slice of type T, executes the passed in predicate function for each element in the slice. If the predicate returns true - the index of the element is returned. If no element is found, -1 is returned. The function is passed the current element, the current index and the slice itself as function arguments.

func FindIndexOf deprecated

func FindIndexOf[T comparable](slice []T, value T) int

FindIndexOf - given a slice of type T and a value of type T, return ths first index of an element equal to value. If no element is found, -1 is returned.

Deprecated: Use slices.Index from the standard library instead:

index := slices.Index(slice, value)

func FindIndexes

func FindIndexes[T any](slice []T, predicate func(value T, index int, slice []T) bool) []int

FindIndexes - given a slice of type T, executes the passed in predicate function for each element in the slice. Returns a slice containing all indexes of elements for which the predicate returned true. If no element are found, returns a nil int slice. The function is passed the current element, the current index and the slice itself as function arguments.

func FindIndexesOf

func FindIndexesOf[T comparable](slice []T, value T) []int

FindIndexesOf - given a slice of type T and a value of type T, returns a slice containing all indexes match the given value. If no element are found, returns a nil int slice.

func FindLastIndex

func FindLastIndex[T any](slice []T, predicate func(value T, index int, slice []T) bool) int

FindLastIndex - given a slice of type T, executes the passed in predicate function for each element in the slice starting from its end. If no element is found, -1 is returned. The function is passed the current element, the current index and the slice itself as function arguments.

func FindLastIndexOf

func FindLastIndexOf[T comparable](slice []T, value T) int

FindLastIndexOf - given a slice of type T and a value of type T, returning the last index matching the given value. If no element is found, -1 is returned.

func FlatMap added in v1.8.0

func FlatMap[T any, R any](slice []T, mapper func(value T, index int, slice []T) []R) []R

FlatMap - given a slice of type T, executes the passed in slice-mapper function for each element in the slice, and returns a flattened slice containing all the elements from all the mapped slices The function is passed the current element, the current index and the slice itself as function arguments.

func Flatten added in v1.5.0

func Flatten[I any](input [][]I) (output []I)

Flatten - receives a slice of slices of type I and flattens it to a slice of type I.

func ForEach

func ForEach[T any](slice []T, function func(value T, index int, slice []T))

ForEach - given a slice of type T, executes the passed in function for each element in the slice. The function is passed the current element, the current index and the slice itself as function arguments.

func GroupBy added in v1.9.0

func GroupBy[T any, K comparable](slice []T, keySelector func(T) K) map[K][]T

GroupBy groups elements of a slice by a key extracted using the keySelector function. Returns a map where keys are the grouping keys and values are slices of elements that share that key. This is a LINQ-style operation useful for categorizing or organizing data.

func Head[T any](slice []T) *T

Head returns a pointer to the first element of the slice. Returns nil if the slice is empty.

func Includes deprecated

func Includes[T comparable](slice []T, value T) bool

Includes - given a slice of type T and a value of type T, determines whether the value is contained by the slice. Note: T is constrained to comparable types only and comparison is determined using the equality operator.

Deprecated: Use slices.Contains from the standard library instead:

found := slices.Contains(slice, value)

func Initial added in v1.9.0

func Initial[T any](slice []T) []T

Initial returns all elements of the slice except the last. Returns an empty slice if the slice has 0 or 1 elements.

func Insert deprecated

func Insert[T any](slice []T, i int, value T) []T

Insert - receives a slice of type T, an index and a value. The value is inserted at the given index. If there is an existing value at this index, it is shifted to the next index. Note: this function does not modify the input slice.

Deprecated: Use slices.Insert from the standard library instead:

inserted := slices.Insert(slice, index, value)

func Intersection

func Intersection[T comparable](slices ...[]T) []T

Intersection - takes a variadic number of slices of type T and returns a slice of type T containing any values that exist in all the slices. For example, given []int{1, 2, 3}, []int{1, 7, 3}, the intersection would be []int{1, 3}.

func Last added in v1.9.0

func Last[T any](slice []T) *T

Last returns a pointer to the last element of the slice. Returns nil if the slice is empty.

func Map

func Map[T any, R any](slice []T, mapper func(value T, index int, slice []T) R) (mapped []R)

Map - given a slice of type T, executes the passed in mapper function for each element in the slice, returning a slice of type R. The function is passed the current element, the current index and the slice itself as function arguments.

func MaxBy added in v1.9.0

func MaxBy[T any, O cmp.Ordered](slice []T, selector func(T) O) *T

MaxBy returns a pointer to the element with the maximum value as determined by the selector function. Returns nil if the slice is empty.

func Merge deprecated

func Merge[T any](inputSlices ...[]T) (mergedSlice []T)

Merge - receives slices of type T and merges them into a single slice of type T. Note: The elements are merged in their order in a slice, i.e. first the elements of the first slice, then that of the second and so forth.

Deprecated: Use slices.Concat from the standard library instead:

merged := slices.Concat(slice1, slice2)

func MinBy added in v1.9.0

func MinBy[T any, O cmp.Ordered](slice []T, selector func(T) O) *T

MinBy returns a pointer to the element with the minimum value as determined by the selector function. Returns nil if the slice is empty.

func Partition added in v1.9.0

func Partition[T any](slice []T, predicate func(T) bool) (truthy []T, falsy []T)

Partition splits a slice into two slices based on a predicate function. The first slice contains elements for which the predicate returns true, the second contains elements for which it returns false.

func Pluck added in v1.2.5

func Pluck[I any, O any](input []I, getter func(I) *O) []O

Pluck - receives a slice of type I and a getter func to a field and returns a slice containing the requested field's value from each item in the slice.

func Reduce

func Reduce[T any, R any](
	slice []T,
	reducer func(acc R, value T, index int, slice []T) R,
	initial R,
) R

Reduce - given a slice of type T, executes the passed in reducer function for each element in the slice, returning a result of type R. The function is passed the accumulator, current element, current index and the slice itself as function arguments. The third argument to reduce is the initial value of type R to use.

func Remove

func Remove[T any](slice []T, i int) []T

Remove - receives a slice of type T and an index, removing the element at the given index. Note: this function does not modify the input slice.

func Reverse

func Reverse[T any](slice []T) []T

Reverse - takes a slice of type T and returns a slice of type T with a reverse order of elements.

func Skip added in v1.9.0

func Skip[T any](slice []T, n int) []T

Skip returns the slice with the first n elements removed. If n is greater than or equal to the slice length, returns an empty slice.

func SkipLast added in v1.9.0

func SkipLast[T any](slice []T, n int) []T

SkipLast returns the slice with the last n elements removed. If n is greater than or equal to the slice length, returns an empty slice.

func SkipWhile added in v1.9.0

func SkipWhile[T any](slice []T, predicate func(T) bool) []T

SkipWhile skips elements from the beginning of the slice while the predicate returns true. Returns the remaining elements starting from the first element where the predicate returns false.

func Some

func Some[T any](slice []T, predicate func(value T, index int, slice []T) bool) bool

Some - given a slice of type T, executes the given predicate for each element of the slice. If the predicate returns true for any element, it returns true, otherwise it returns false. The function is passed the current element, the current index and the slice itself as function arguments.

func Sum

func Sum[T constraints.Complex | constraints.Integer | constraints.Float](slice []T) (result T)

Sum - receives a slice of type T and returns a value T that is the sum of the numbers. Note: T is constrained to be a number type.

func Tail added in v1.9.0

func Tail[T any](slice []T) []T

Tail returns all elements of the slice except the first. Returns an empty slice if the slice has 0 or 1 elements.

func Take added in v1.9.0

func Take[T any](slice []T, n int) []T

Take returns the first n elements from the slice. If n is greater than the slice length, returns the entire slice.

func TakeLast added in v1.9.0

func TakeLast[T any](slice []T, n int) []T

TakeLast returns the last n elements from the slice. If n is greater than the slice length, returns the entire slice.

func TakeWhile added in v1.9.0

func TakeWhile[T any](slice []T, predicate func(T) bool) []T

TakeWhile returns elements from the beginning of the slice while the predicate returns true. Stops at the first element where the predicate returns false.

func Union

func Union[T comparable](inputSlices ...[]T) []T

Union - takes a variadic number of slices of type T and returns a slice of type T containing the unique elements in the different slices For example, given []int{1, 2, 3}, []int{2, 3, 4}, []int{3, 4, 5}, the union would be []int{1, 2, 3, 4, 5}.

func Unique

func Unique[T comparable](slice []T) []T

Unique - receives a slice of type T and returns a slice of type T containing all unique elements.

func Windows added in v1.9.0

func Windows[T any](slice []T, size int) [][]T

Windows returns a slice of sliding windows of the specified size. Each window is a slice of consecutive elements. If size is greater than the slice length, returns an empty slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL