Documentation ¶
Index ¶
- func All[T any](collection []T, f func(T) bool) bool
- func KeyBy[T any, K comparable](collection []T, keygen func(T) K) map[K]T
- func Map[T any, R any](collection []T, mapper func(T) R) []R
- func Reduce[T, R any](collection []T, reducer func(T, R) R, initial R) R
- func Reject[T any](collection []T, rejecter func(T) bool) []T
- func Select[T any](collection []T, selecter func(T) bool) []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if all elements in collection return true when passed to f, otherwise it returns false.
Example ¶
collection := []int{1, 2, 3} res := All(collection, func(i int) bool { return i > 0 }) fmt.Println(res)
Output: true
func KeyBy ¶
func KeyBy[T any, K comparable](collection []T, keygen func(T) K) map[K]T
KeyBy calls keygen on each element of collection, setting the result of f as the key in the returned map, and the element as the value.
Example ¶
collection := []string{"first", "second", "last"} res := KeyBy(collection, func(s string) int { return len(s) }) fmt.Println(res)
Output: map[4:last 5:first 6:second]
func Map ¶
Map calls mapper on each element on collection, returning a new collection with the results of each call to mapper.
Example ¶
collection := []int{1, 2, 3} res := Map(collection, func(i int) int { return i * 2 }) fmt.Println(res)
Output: [2 4 6]
func Reduce ¶
func Reduce[T, R any](collection []T, reducer func(T, R) R, initial R) R
Reduce accepts a slice of values, a function that accepts an element of the slice and an accumulater, and an initial value for the accumulator. The result of each call to reducer is passed as the value to the next iteration.
Example ¶
collection := []int{1, 2, 3} res := Reduce(collection, func(i, acc int) int { return acc + i }, 0) fmt.Println(res)
Output: 6
func Reject ¶
Reject calls rejecter on each element of collection, returning a new slice for all elements where f returns false.
Example ¶
collection := []int{1, 2, 3, 4} res := Reject(collection, func(i int) bool { return i%2 == 0 }) fmt.Println(res)
Output: [1 3]
Types ¶
This section is empty.