sliceutils

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2024 License: MIT Imports: 1 Imported by: 2

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 Copy

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

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

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

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

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

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 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

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.

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

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 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

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 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 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.

Jump to

Keyboard shortcuts

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