Documentation
¶
Overview ¶
Package arr provides utility functions for array/slice manipulation.
Index ¶
- func Chunk[T any](array []T, size int) [][]T
- func Compact[T comparable](array []T) []T
- func Concat[T any](arrays ...[]T) []T
- func Difference[T comparable](array []T, others ...[]T) []T
- func Drop[T any](array []T, n int) []T
- func DropRight[T any](array []T, n int) []T
- func Fill[T any](array []T, value T, start, end int) []T
- func FindIndex[T any](array []T, predicate func(T) bool) int
- func FindLastIndex[T any](array []T, predicate func(T) bool) int
- func First[T any](array []T) (T, bool)
- func Flatten[T any](array [][]T) []T
- func Includes[T comparable](array []T, value T) bool
- func IndexOf[T comparable](array []T, value T) int
- func Initial[T any](array []T) []T
- func Intersection[T comparable](arrays ...[]T) []T
- func Join[T any](array []T, separator string) string
- func Last[T any](array []T) (T, bool)
- func LastIndexOf[T comparable](array []T, value T) int
- func Nth[T any](array []T, n int) (T, bool)
- func Pull[T comparable](array []T, values ...T) []T
- func Random[T any](slice []T, n int) []T
- func RandomChoice[T any](choices []T) (T, bool)
- func Reverse[T any](array []T) []T
- func Shuffle[T any](slice []T) []T
- func Slice[T any](array []T, start, end int) []T
- func SortBy[T any, U int | int8 | int16 | int32 | int64 | float32 | float64 | string](array []T, iteratee func(T) U) []T
- func SortedIndex[T int | int8 | int16 | int32 | int64 | float32 | float64](array []T, value T) int
- func Tail[T any](array []T) []T
- func Take[T any](array []T, n int) []T
- func TakeRight[T any](array []T, n int) []T
- func Union[T comparable](arrays ...[]T) []T
- func Uniq[T comparable](array []T) []T
- func Without[T comparable](array []T, values ...T) []T
- func Zip[T any](arrays ...[]T) [][]T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶
Chunk splits an array into groups of the specified size. Example: Chunk([]int{1, 2, 3, 4}, 2) -> [][]int{{1, 2}, {3, 4}}
func Compact ¶
func Compact[T comparable](array []T) []T
Compact removes falsey values from an array. In Go, we consider nil, zero values, and empty collections as falsey. Example: Compact([]int{0, 1, 2, 0, 3}) -> []int{1, 2, 3}
func Concat ¶
func Concat[T any](arrays ...[]T) []T
Concat concatenates arrays together. Example: Concat([]int{1, 2}, []int{3, 4}) -> []int{1, 2, 3, 4}
func Difference ¶
func Difference[T comparable](array []T, others ...[]T) []T
Difference returns an array of elements that are in the first array but not in the others. Example: Difference([]int{1, 2, 3}, []int{2, 3, 4}) -> []int{1}
func Drop ¶
Drop creates a slice with n elements dropped from the beginning. Example: Drop([]int{1, 2, 3, 4}, 2) -> []int{3, 4}
func DropRight ¶
DropRight creates a slice with n elements dropped from the end. Example: DropRight([]int{1, 2, 3, 4}, 2) -> []int{1, 2}
func Fill ¶
Fill fills elements of array with value from start up to, but not including, end. Example: Fill([]int{1, 2, 3, 4}, 0, 1, 3) -> []int{1, 0, 0, 4}
func FindIndex ¶
FindIndex returns the index of the first element that satisfies the predicate function. Example: FindIndex([]int{1, 2, 3, 4}, func(n int) bool { return n > 2 }) -> 2
func FindLastIndex ¶
FindLastIndex returns the index of the last element that satisfies the predicate function. Example: FindLastIndex([]int{1, 2, 3, 4}, func(n int) bool { return n > 2 }) -> 3
func Flatten ¶
func Flatten[T any](array [][]T) []T
Flatten flattens an array a single level deep. Example: Flatten([][]int{{1, 2}, {3, 4}}) -> []int{1, 2, 3, 4}
func Includes ¶
func Includes[T comparable](array []T, value T) bool
Includes checks if a value is in the array. Example: Includes([]int{1, 2, 3}, 2) -> true
func IndexOf ¶
func IndexOf[T comparable](array []T, value T) int
IndexOf returns the index of the first occurrence of value in array. Example: IndexOf([]int{1, 2, 3, 2}, 2) -> 1
func Initial ¶
func Initial[T any](array []T) []T
Initial returns all but the last element of an array. Example: Initial([]int{1, 2, 3}) -> []int{1, 2}
func Intersection ¶
func Intersection[T comparable](arrays ...[]T) []T
Intersection returns an array of unique values that are included in all given arrays. Example: Intersection([]int{1, 2, 3}, []int{2, 3, 4}) -> []int{2, 3}
func Join ¶
Join joins all elements of an array into a string. Example: Join([]int{1, 2, 3}, ",") -> "1,2,3"
func LastIndexOf ¶
func LastIndexOf[T comparable](array []T, value T) int
LastIndexOf returns the index of the last occurrence of value in array. Example: LastIndexOf([]int{1, 2, 3, 2}, 2) -> 3
func Nth ¶
Nth returns the element at index n of array. If n is negative, the nth element from the end is returned. Example: Nth([]int{1, 2, 3}, 1) -> 2
func Pull ¶
func Pull[T comparable](array []T, values ...T) []T
Pull removes all given values from array. Example: Pull([]int{1, 2, 3, 1, 2, 3}, 2, 3) -> []int{1, 1}
func Random ¶
Random returns n random elements from the given slice without replacement. Example: Random([]int{1, 2, 3, 4, 5}, 3) -> [2, 4, 1]
func RandomChoice ¶
RandomChoice returns a random element from the given slice. Example: RandomChoice([]string{"a", "b", "c"}) -> "b"
func Reverse ¶
func Reverse[T any](array []T) []T
Reverse reverses the order of elements in array. Example: Reverse([]int{1, 2, 3}) -> []int{3, 2, 1}
func Shuffle ¶
func Shuffle[T any](slice []T) []T
Shuffle returns a new slice with elements in random order. Example: Shuffle([]int{1, 2, 3, 4, 5}) -> [3, 1, 5, 2, 4]
func Slice ¶
Slice returns a slice of array from start up to, but not including, end. Example: Slice([]int{1, 2, 3, 4}, 1, 3) -> []int{2, 3}
func SortBy ¶
func SortBy[T any, U int | int8 | int16 | int32 | int64 | float32 | float64 | string](array []T, iteratee func(T) U) []T
SortBy sorts an array by the results of running each element through iteratee. Example: SortBy([]int{1, 3, 2}, func(n int) int { return n })
func SortedIndex ¶
SortedIndex returns the index at which value should be inserted into array to maintain its sort order. Example: SortedIndex([]int{1, 3, 5, 7}, 4) -> 2
func Tail ¶
func Tail[T any](array []T) []T
Tail returns all but the first element of array. Example: Tail([]int{1, 2, 3}) -> []int{2, 3}
func Take ¶
Take creates a slice of array with n elements taken from the beginning. Example: Take([]int{1, 2, 3, 4}, 2) -> []int{1, 2}
func TakeRight ¶
TakeRight creates a slice of array with n elements taken from the end. Example: TakeRight([]int{1, 2, 3, 4}, 2) -> []int{3, 4}
func Union ¶
func Union[T comparable](arrays ...[]T) []T
Union creates an array of unique values from all given arrays. Example: Union([]int{1, 2}, []int{2, 3}) -> []int{1, 2, 3}
func Uniq ¶
func Uniq[T comparable](array []T) []T
Uniq creates an array of unique values. Example: Uniq([]int{1, 2, 1, 3}) -> []int{1, 2, 3}
func Without ¶
func Without[T comparable](array []T, values ...T) []T
Without creates an array excluding all given values. Example: Without([]int{1, 2, 3, 4}, 2, 4) -> []int{1, 3}
Types ¶
This section is empty.