Documentation ¶
Index ¶
- func Chunk[T any](input []T, size int) [][]T
- func Copy[T any](slice []T) []T
- func Difference[T comparable](slices ...[]T) []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) int
- 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 Includes[T comparable](slice []T, value T) bool
- func Insert[T any](slice []T, i int, value T) []T
- func Intersection[T comparable](slices ...[]T) []T
- func Map[T any, R any](slice []T, mapper func(value T, index int, slice []T) R) (mapped []R)
- func Merge[T any](slices ...[]T) (mergedSlice []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 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 Union[T comparable](slices ...[]T) []T
- func Unique[T comparable](slice []T) []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 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 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 ¶
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.
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 Includes ¶
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.
func Insert ¶
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.
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 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 Merge ¶
func Merge[T any](slices ...[]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.
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 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 Union ¶
func Union[T comparable](slices ...[]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.