Documentation
¶
Overview ¶
Package arr contains functions to interact with arrays, slices, and sometimes maps.
The package is imported like this:
import "github.com/gouniverse/base/arr"
Index ¶
- func Contains[T comparable](slice []T, item T) bool
- func ContainsAll[T comparable](a, b []T) bool
- func Count[T comparable](collection []T) (count int)
- func CountBy[T any](collection []T, predicate func(item T) bool) (count int)
- func Each[T any](collection []T, iteratee func(item T, index int))
- func Equals[T comparable](a, b []T) bool
- func Filter[V any](collection []V, predicate func(item V, index int) bool) []V
- func FilterEmpty(slice []string) []string
- func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T
- func Index[T comparable](slice []T, item T) int
- func IndexMoveDown[T any](slice []T, index int) []T
- func IndexMoveUp[T any](slice []T, index int) []T
- func IndexRemove[T any](slice []T, index int) []T
- func Keys[K comparable, V any](in map[K]V) []K
- func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R
- func Max[T constraints.Ordered](collection []T) T
- func Merge[T any](slices ...[]T) []T
- func Min[T constraints.Ordered](collection []T) T
- func Random[T any](slice []T) T
- func Reverse[T any](collection []T) []T
- func Shuffle[T any](collection []T) []T
- func Split[T any](collection []T, size int) [][]T
- func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T
- func Unique[T comparable](collection []T) []T
- func Values[K comparable, V any](in map[K]V) []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](slice []T, item T) bool
func ContainsAll ¶
func ContainsAll[T comparable](a, b []T) bool
ContainsAll checks if slice 'a' contains all elements of slice 'b', regardless of order.
func Count ¶
func Count[T comparable](collection []T) (count int)
Count counts the number of elements in the collection.
func Equals ¶
func Equals[T comparable](a, b []T) bool
Equals checks if two slices are equal.
This function assumes that the slices contain comparable types.
It first checks if the slices have the same length. If not, it immediately returns false.
If the slices have the same length, it then checks if the elements are equal. If it finds an element that is not equal, it immediately returns false.
If it checks all elements and finds no unequal elements, it returns true.
func Filter ¶
Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
func FilterEmpty ¶
FilterEmpty takes a slice of strings and returns a new slice with all empty strings removed.
Example:
arr.FilterEmpty([]string{"", "hello", "", "world"}) // returns []string{"hello", "world"}
Parameters: - slice: the slice of strings to filter.
Returns: - []string: a new slice with all empty strings removed.
func GroupBy ¶
func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T
GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee.
func Index ¶
func Index[T comparable](slice []T, item T) int
Index finds the index of the first occurrence of an item in a slice.
Returns -1 if the item is not found.
func IndexMoveDown ¶
indexMoveDown moves the element at the given index down
Business logic: - if the index is last index, will be ignored - if the index out of bounds, will be ignored
Parameters: - slice: the slice to move the element from - index: the index of the element to move
Returns: - []T: the new slice
Example:
arr := []int{1, 2, 3, 4}
result := IndexMoveDown(arr, 1)
// result is now [1, 4, 2, 3]
func IndexMoveUp ¶
indexMoveUp moves the element at the given index up
Business logic: - if the index is first index, will be ignored - if the index out of bounds, will be ignored
Parameters: - slice: the slice to move the element from - index: the index of the element to move
Returns: - []T: the new slice
Example:
indices := []int{1, 2, 3, 4, 5}
IndexMoveUp(indices, 2)
fmt.Println(indices) // [1, 3, 2, 4, 5]
func IndexRemove ¶
IndexRemove removes the element at the given index from the slice.
Parameters: - slice: the slice to remove the element from. - index: the index of the element to remove.
Returns: - []T: a new slice with the element at the given index removed.
Business Logic: - If the index is out of bounds, the original slice is returned unchanged. - This function does not panic on an out-of-bounds index.
Example:
arr := []int{1, 2, 3, 4}
result := IndexRemove(arr, 2)
// result is now [1, 2, 4]
func Max ¶
func Max[T constraints.Ordered](collection []T) T
Max searches the maximum value of a collection.
func Min ¶
func Min[T constraints.Ordered](collection []T) T
Min search the minimum value of a collection.
func Random ¶
func Random[T any](slice []T) T
Random returns a random element from the provided slice. It uses Go generics, so it works with slices of any comparable type T. If the slice is empty, it returns the zero value of type T and false.
func Reverse ¶
func Reverse[T any](collection []T) []T
Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
func Shuffle ¶
func Shuffle[T any](collection []T) []T
Shuffle returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
func Split ¶
Split returns an array of elements split into groups the length of size. If array can't be split evenly,
func Sum ¶
func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T
Sum sums the values in a collection. If collection is empty 0 is returned.
func Unique ¶
func Unique[T comparable](collection []T) []T
Unique returns a duplicate-free version of an array, in which only the first occurrence of each element is kept.
func Values ¶
func Values[K comparable, V any](in map[K]V) []V
Values creates an array of the map values.
Types ¶
This section is empty.