Documentation
¶
Index ¶
- func Chunk[T any](input []T, size int) [][]T
- func Compact[T comparable](slice []T) []T
- func Copy[T any](slice []T) []Tdeprecated
- func CountBy[T any, K comparable](slice []T, keySelector func(T) K) map[K]int
- func Difference[T comparable](slices ...[]T) []T
- func DistinctBy[T any, K comparable](slice []T, keySelector func(T) K) []T
- func EnsureUniqueAndAppend[T comparable](slice []T, item T) []T
- func Every[T any](slice []T, predicate func(value T, index int, slice []T) bool) bool
- func Filter[T any](slice []T, predicate func(value T, index int, slice []T) bool) (filtered []T)
- func Find[T any](slice []T, predicate func(value T, index int, slice []T) bool) *T
- func FindIndex[T any](slice []T, predicate func(value T, index int, slice []T) bool) int
- func FindIndexOf[T comparable](slice []T, value T) intdeprecated
- func FindIndexes[T any](slice []T, predicate func(value T, index int, slice []T) bool) []int
- func FindIndexesOf[T comparable](slice []T, value T) []int
- func FindLastIndex[T any](slice []T, predicate func(value T, index int, slice []T) bool) int
- func FindLastIndexOf[T comparable](slice []T, value T) int
- func FlatMap[T any, R any](slice []T, mapper func(value T, index int, slice []T) []R) []R
- func Flatten[I any](input [][]I) (output []I)
- func ForEach[T any](slice []T, function func(value T, index int, slice []T))
- func GroupBy[T any, K comparable](slice []T, keySelector func(T) K) map[K][]T
- func Head[T any](slice []T) *T
- func Includes[T comparable](slice []T, value T) booldeprecated
- func Initial[T any](slice []T) []T
- func Insert[T any](slice []T, i int, value T) []Tdeprecated
- func Intersection[T comparable](slices ...[]T) []T
- func Last[T any](slice []T) *T
- func Map[T any, R any](slice []T, mapper func(value T, index int, slice []T) R) (mapped []R)
- func MaxBy[T any, O cmp.Ordered](slice []T, selector func(T) O) *T
- func Merge[T any](inputSlices ...[]T) (mergedSlice []T)deprecated
- func MinBy[T any, O cmp.Ordered](slice []T, selector func(T) O) *T
- func Partition[T any](slice []T, predicate func(T) bool) (truthy []T, falsy []T)
- func Pluck[I any, O any](input []I, getter func(I) *O) []O
- func Reduce[T any, R any](slice []T, reducer func(acc R, value T, index int, slice []T) R, initial R) R
- func Remove[T any](slice []T, i int) []T
- func Reverse[T any](slice []T) []T
- func Skip[T any](slice []T, n int) []T
- func SkipLast[T any](slice []T, n int) []T
- func SkipWhile[T any](slice []T, predicate func(T) bool) []T
- func Some[T any](slice []T, predicate func(value T, index int, slice []T) bool) bool
- func Sum[T constraints.Complex | constraints.Integer | constraints.Float](slice []T) (result T)
- func Tail[T any](slice []T) []T
- func Take[T any](slice []T, n int) []T
- func TakeLast[T any](slice []T, n int) []T
- func TakeWhile[T any](slice []T, predicate func(T) bool) []T
- func Union[T comparable](inputSlices ...[]T) []T
- func Unique[T comparable](slice []T) []T
- func Windows[T any](slice []T, size int) [][]T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶ added in v1.2.2
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 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶ added in v1.9.0
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
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 ¶
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
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
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
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
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 ¶
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
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
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
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 ¶
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
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
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
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.
Types ¶
This section is empty.