Documentation
¶
Overview ¶
Package coll provides utility functions for collection manipulation. In Go, collections can be slices or maps.
Index ¶
- func CountBy[T any, K comparable](collection []T, iteratee func(T) K) map[K]int
- func Every[T any](collection []T, predicate func(T) bool) bool
- func Filter[T any](collection []T, predicate func(T) bool) []T
- func FilterMap[K comparable, V any](collection map[K]V, predicate func(V, K) bool) map[K]V
- func Find[T any](collection []T, predicate func(T) bool) (T, bool)
- func FindLast[T any](collection []T, predicate func(T) bool) (T, bool)
- func ForEach[T any](collection []T, iteratee func(T))
- func ForEachMap[K comparable, V any](collection map[K]V, iteratee func(V, K))
- func ForEachWithIndex[T any](collection []T, iteratee func(T, int))
- func GroupBy[T any, K comparable](collection []T, iteratee func(T) K) map[K][]T
- func Includes[T comparable](collection []T, value T) bool
- func KeyBy[T any, K comparable](collection []T, iteratee func(T) K) map[K]T
- func Map[T any, R any](collection []T, iteratee func(T) R) []R
- func MapMap[K comparable, V any, R any](collection map[K]V, iteratee func(V, K) R) []R
- func MapWithIndex[T any, R any](collection []T, iteratee func(T, int) R) []R
- func OrderBy[T any, U int | int8 | int16 | int32 | int64 | float32 | float64 | string](collection []T, iteratee func(T) U, ascending bool) []T
- func Partition[T any](collection []T, predicate func(T) bool) [][]T
- func Reduce[T any, R any](collection []T, iteratee func(R, T) R, accumulator R) R
- func ReduceMap[K comparable, V any, R any](collection map[K]V, iteratee func(R, V, K) R, accumulator R) R
- func ReduceRight[T any, R any](collection []T, iteratee func(R, T) R, accumulator R) R
- func Reject[T any](collection []T, predicate func(T) bool) []T
- func Sample[T any](collection []T) (T, bool)
- func SampleSize[T any](collection []T, n int) []T
- func Size[T any](collection []T) int
- func Some[T any](collection []T, predicate func(T) bool) bool
- func SortBy[T any, U int | int8 | int16 | int32 | int64 | float32 | float64 | string](collection []T, iteratee func(T) U) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountBy ¶
func CountBy[T any, K comparable](collection []T, iteratee func(T) K) map[K]int
CountBy counts the occurrences of elements in a collection based on the iteratee function. Example: CountBy([]int{1, 2, 3, 4}, func(n int) string { return "even" if n % 2 == 0 else "odd" }) -> map[string]int{"odd": 2, "even": 2}
func Every ¶
Every checks if all elements in the collection satisfy the predicate. Example: Every([]int{2, 4, 6}, func(n int) bool { return n % 2 == 0 }) -> true
func Filter ¶
Filter filters elements of a collection that satisfy the predicate. Example: Filter([]int{1, 2, 3, 4}, func(n int) bool { return n % 2 == 0 }) -> []int{2, 4}
func FilterMap ¶
func FilterMap[K comparable, V any](collection map[K]V, predicate func(V, K) bool) map[K]V
FilterMap filters elements of a map that satisfy the predicate. Example: FilterMap(map[string]int{"a": 1, "b": 2}, func(v int, k string) bool { return v > 1 }) -> map[string]int{"b": 2}
func Find ¶
Find finds the first element in the collection that satisfies the predicate. Example: Find([]int{1, 2, 3, 4}, func(n int) bool { return n > 2 }) -> 3, true
func FindLast ¶
FindLast finds the last element in the collection that satisfies the predicate. Example: FindLast([]int{1, 2, 3, 4}, func(n int) bool { return n > 2 }) -> 4, true
func ForEach ¶
func ForEach[T any](collection []T, iteratee func(T))
ForEach iterates over elements of a collection and invokes iteratee for each element. Example: ForEach([]int{1, 2, 3}, func(n int) { fmt.Println(n) })
func ForEachMap ¶
func ForEachMap[K comparable, V any](collection map[K]V, iteratee func(V, K))
ForEachMap iterates over elements of a map and invokes iteratee for each element. Example: ForEachMap(map[string]int{"a": 1, "b": 2}, func(v int, k string) { fmt.Println(k, v) })
func ForEachWithIndex ¶
ForEachWithIndex iterates over elements of a collection and invokes iteratee for each element with its index. Example: ForEachWithIndex([]int{1, 2, 3}, func(n int, i int) { fmt.Println(i, n) })
func GroupBy ¶
func GroupBy[T any, K comparable](collection []T, iteratee func(T) K) map[K][]T
GroupBy creates an object composed of keys generated from the results of running each element of collection through iteratee. Example: GroupBy([]int{1, 2, 3, 4}, func(n int) string { return "even" if n % 2 == 0 else "odd" }) -> map[string][]int{"odd": {1, 3}, "even": {2, 4}}
func Includes ¶
func Includes[T comparable](collection []T, value T) bool
Includes checks if a value is in the collection. Example: Includes([]int{1, 2, 3}, 2) -> true
func KeyBy ¶
func KeyBy[T any, K comparable](collection []T, iteratee func(T) K) map[K]T
KeyBy creates an object composed of keys generated from the results of running each element of collection through iteratee. Example: KeyBy([]struct{ID int, Name string}{{1, "Alice"}, {2, "Bob"}}, func(u User) int { return u.ID }) -> map[int]struct{ID int, Name string}{1: {1, "Alice"}, 2: {2, "Bob"}}
func Map ¶
Map creates an array of values by running each element in collection through iteratee. Example: Map([]int{1, 2, 3}, func(n int) int { return n * 2 }) -> []int{2, 4, 6}
func MapMap ¶
func MapMap[K comparable, V any, R any](collection map[K]V, iteratee func(V, K) R) []R
MapMap creates an array of values by running each element in a map through iteratee. Example: MapMap(map[string]int{"a": 1, "b": 2}, func(v int, k string) string { return k + strconv.Itoa(v) }) -> []string{"a1", "b2"}
func MapWithIndex ¶
MapWithIndex creates an array of values by running each element in collection through iteratee with its index. Example: MapWithIndex([]int{1, 2, 3}, func(n, i int) int { return n * i }) -> []int{0, 2, 6}
func OrderBy ¶
func OrderBy[T any, U int | int8 | int16 | int32 | int64 | float32 | float64 | string](collection []T, iteratee func(T) U, ascending bool) []T
OrderBy sorts a collection by multiple iteratees. Example: OrderBy([]User{{Name: "fred", Age: 48}, {Name: "barney", Age: 34}}, []string{"age", "name"}, []bool{true, false}) This is a complex function that's hard to implement in Go due to type system limitations. For now, we'll just provide a simplified version that sorts by a single iteratee.
func Partition ¶
Partition splits a collection into two groups, the first of which contains elements that satisfy the predicate. Example: Partition([]int{1, 2, 3, 4}, func(n int) bool { return n % 2 == 0 }) -> [][]int{{2, 4}, {1, 3}}
func Reduce ¶
Reduce reduces a collection to a value by iterating through the collection and applying an accumulator function. Example: Reduce([]int{1, 2, 3}, func(sum, n int) int { return sum + n }, 0) -> 6
func ReduceMap ¶
func ReduceMap[K comparable, V any, R any](collection map[K]V, iteratee func(R, V, K) R, accumulator R) R
ReduceMap reduces a map to a value by iterating through the map and applying an accumulator function. Example: ReduceMap(map[string]int{"a": 1, "b": 2}, func(sum int, v int, k string) int { return sum + v }, 0) -> 3
func ReduceRight ¶
ReduceRight reduces a collection to a value by iterating through the collection from right to left. Example: ReduceRight([]int{1, 2, 3}, func(result, n int) int { return result * 10 + n }, 0) -> 321
func Reject ¶
Reject is the opposite of Filter; it returns elements that don't satisfy the predicate. Example: Reject([]int{1, 2, 3, 4}, func(n int) bool { return n % 2 == 0 }) -> []int{1, 3}
func Sample ¶
Sample gets a random element from a collection. Example: Sample([]int{1, 2, 3, 4}) -> a random element from the collection
func SampleSize ¶
SampleSize gets n random elements from a collection. Example: SampleSize([]int{1, 2, 3, 4}, 2) -> []int{2, 4} (random elements)
func Some ¶
Some checks if any element in the collection satisfies the predicate. Example: Some([]int{1, 2, 3, 4}, func(n int) bool { return n > 3 }) -> true
func SortBy ¶
func SortBy[T any, U int | int8 | int16 | int32 | int64 | float32 | float64 | string](collection []T, iteratee func(T) U) []T
SortBy sorts a collection by the results of running each element through iteratee. Example: SortBy([]int{1, 3, 2}, func(n int) int { return n }) -> []int{1, 2, 3}
Types ¶
This section is empty.