Documentation ¶
Index ¶
- Constants
- func BoundedSub[T any](slice []T, start, end int) []T
- func Chunk[T any](slice []T, chunkSize int) [][]T
- func Compact[T any](slice []T) []T
- func Contains[T any](slice []T, item T) bool
- func ContainsBy[T any](slice []T, predicate func(T) bool) bool
- func Filter[T any](slice []T, predicate func(T) bool) []T
- func Flatten[T any](slices [][]T) []T
- func ForEach[T any](slice []T, iterator func(item T))
- func ForEachWithIndex[T any](slice []T, iterator func(item T, index int))
- func ForEachWithStop[T any](slice []T, iterator func(item T, stop func()))
- func IndexOf[T any](slice []T, item T, fromIndex ...int) int
- func Len[T any](slices ...[]T) int
- func Map[T any, R any](slice []T, mapFunc func(item T) R) []R
- func MapWithIndex[T any, R any](slice []T, mapFunc func(item T, index int) R) []R
- func Merge[T any](slices ...[]T) []T
- func Order[T cmp.Ordered](slice []T, orders ...SortingOrder) []T
- func RegulateBounds(start int, end int, length int) (int, int)
- func Reverse[T any](slice []T) []T
- func Rotate[T any](slice []T, i int) []T
- func Shuffle[T any](slice []T) []T
- func Sort[T cmp.Ordered](slice []T) []T
- func Sub[T any](slice []T, start, end int) []T
- func Unique[T comparable](slices ...[]T) []T
- type SortingOrder
Constants ¶
const NotFound = -1
NotFound represents the value -1 which is commonly used to indicate that a requested item or index is not found in a collection or container.
Variables ¶
This section is empty.
Functions ¶
func BoundedSub ¶
BoundedSub returns a sub-slice of a given slice, bounded by start and end indices. The start index is inclusive, while the end index is exclusive. If the start index is negative, it will be set to 0. If the end index is greater than the length of the slice, it will be set to the length of the slice. If the end index is less than the start index, the sub-slice will be empty.
Example usage:
slice := []int{1, 2, 3, 4, 5} start := 1 end := 3 result := BoundedSub(slice, start, end) // []int{2, 3}
func Compact ¶
func Compact[T any](slice []T) []T
Compact creates a slice with all falsy values removed.
func ContainsBy ¶
ContainsBy checks if an item is present in a given slice.
func Filter ¶
Filter takes a slice and a predicate function as input parameters and returns a new slice containing only the elements that satisfy the predicate function.
func Flatten ¶
func Flatten[T any](slices [][]T) []T
Flatten takes a slice of slices of any type and returns a flattened slice of the same type.
func ForEach ¶
func ForEach[T any](slice []T, iterator func(item T))
ForEach applies a given function to each element of a slice.
func ForEachWithIndex ¶ added in v0.1.4
ForEachWithIndex iterates over a slice and applies the specified map function to each element, providing the element value and its index as arguments to the map function.
func ForEachWithStop ¶ added in v0.1.4
func ForEachWithStop[T any](slice []T, iterator func(item T, stop func()))
ForEachWithStop applies a provided function to each element in a slice of any type. It allows the function to stop iteration by invoking the provided stop function.
func IndexOf ¶
IndexOf finds the index of an item in a slice. If fromIndex is provided, the search starts from that index. Returns -1 if the item is not found.
func Map ¶
Map applies a function to each element of a slice and returns a new slice containing the results.
func MapWithIndex ¶ added in v0.1.4
MapWithIndex applies the given map function to each item in the slice along with its index.
func Merge ¶
func Merge[T any](slices ...[]T) []T
Merge takes in multiple slices of any type and merges them into a single slice.
Example usage:
slice1 := []int{1, 2, 3} slice2 := []int{4, 5, 6} merged := Merge(slice1, slice2) // Result: [1, 2, 3, 4, 5, 6]
Parameters:
slices: Multiple slices of any type to be merged.
Returns:
The merged slice containing elements from all the input slices.
func Order ¶
func Order[T cmp.Ordered](slice []T, orders ...SortingOrder) []T
Order sorts a given slice of elements in ascending or descending order based on the provided sorting order.
The slice is not modified, and a new sorted slice is returned.
The sorting order can be specified as optional arguments.
If no sorting order is provided, the default sorting order is ascending.
func RegulateBounds ¶
RegulateBounds returns the regulated start and end bounds within the given length. If the start index is negative, it will be set to 0. If the end index is greater than length, it will be set to length. If the end index is less than the start index, it will be set equal to start.
func Reverse ¶
func Reverse[T any](slice []T) []T
Reverse reverses the order of elements in a slice.
func Rotate ¶
Rotate rotates the elements of a slice by a given number of positions. The rotate operation wraps around the slice, so elements shifted beyond the end of the slice are moved to the beginning. The original slice is not modified and a new rotated slice is returned.
Parameters:
- slice: The slice to rotate.
- i: The number of positions to rotate the slice by. Positive values rotate the slice to the right, while negative values rotate it to the left.
Returns:
The rotated slice.
func Shuffle ¶
func Shuffle[T any](slice []T) []T
Shuffle randomly reorders the elements in the given slice. It creates a new slice with the same length as the input slice and then shuffles the elements by swapping them with a randomly selected element before it. The function uses the Fisher-Yates algorithm to ensure a uniform distribution of the shuffled elements.
The input slice is not modified by the shuffle operation. Instead, a new shuffled slice is created and returned.
Example usage:
numbers := []int{1, 2, 3, 4, 5} shuffled := Shuffle(numbers) fmt.Println(shuffled) // Example Output: [2, 5, 4, 1, 3]
This function has a time complexity of O(n), where n is the length of the input slice.
func Sub ¶
Sub takes a slice and returns a sub-slice from the specified start index to the end index (exclusive). The start index is inclusive, while the end index is exclusive.
Example usage:
slice := []int{1, 2, 3, 4, 5, 6} subSlice := Sub(slice, 2, 4) // subSlice = [3, 4]
func Unique ¶
func Unique[T comparable](slices ...[]T) []T
Unique takes in one or more slices of comparable elements and returns a new slice containing only the unique elements from the input slices.
Types ¶
type SortingOrder ¶
type SortingOrder string
SortingOrder represents the order in which to sort a collection of items.
SortingOrder values can be "asc" (ascending) or "desc" (descending).
const ( Asc SortingOrder = "asc" Desc SortingOrder = "desc" )