Documentation ¶
Overview ¶
Package generics implements common generic functions.
Since many of the types are not specified at compile-time, each function's documentation will begin with its equivalent generic type definition. For example, for the Map function, the type definition is:
func Map(slc []T, pred func(T) U) []U
For all functions, if the types of the arguments do not match the generic argument types of the function, it will cause a runtime panic.
Except in certain documented cases, the documented return types are guaranteed to be valid. Thus, type assertions are guaranteed to succeed. For example, for func Identity(x T) T:
yVal := Identity(1) y, _ := yVal.(int) // guaranteed to succeed
Index ¶
- func Count(slc, pred interface{}) int
- func Every(slc, pred interface{}) bool
- func Filter(slc, pred interface{}) interface{}
- func Find(slc, pred interface{}) interface{}
- func FindIndex(slc, pred interface{}) int
- func Foldl(slc, zero, pred interface{}) interface{}
- func Foldr(slc, zero, pred interface{}) interface{}
- func Identity(x interface{}) interface{}
- func Map(slc, pred interface{}) interface{}
- func Max(slc, less interface{}) interface{}
- func Min(slc, less interface{}) interface{}
- func Reject(slc, pred interface{}) interface{}
- func Some(slc, pred interface{}) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Count ¶
func Count(slc, pred interface{}) int
func Count(slc []T, pred func(T) bool) int
Count applies pred to each element in slc, and returns the number of elements for which the call returned true.
func Every ¶
func Every(slc, pred interface{}) bool
func Every(slc []T, pred func(T) bool) bool
Every applies pred to each element in slc. If any of those calls returns false, Every returns false. Otherwise, it returns true.
func Filter ¶
func Filter(slc, pred interface{}) interface{}
func Filter(slc []T, pred func(T) bool) []T
Filter applies pred to each element of slc, and returns those elements for which pred returned true. The returned slice will be only as long as it needs to to store all "true" elements, not necessarily as long as slc.
func Find ¶
func Find(slc, pred interface{}) interface{}
func Find(slc []T, pred func(T) bool) T
Find applies pred to each element in slc, returning the first element for which pred returns true. If pred never returns true, Find returns a nil interface. Note that type assertions of the form
f := find([]int{1, 2, 3}, pred) g, ok := f.(int)
may fail, which breaks the contract which most other functions in this package obey.
func FindIndex ¶
func FindIndex(slc, pred interface{}) int
func FindIndex(slc []T, pred func(T) bool) int
FindIndex applies pred to each element in slc, returning the index of the first element for which pred returns true. If pred never returns true, FindIndex returns -1.
func Foldl ¶
func Foldl(slc, zero, pred interface{}) interface{}
func Foldl(slc []T, zero U, pred func(U, T) U) U
Foldr applies pred to each element of slc in reverse order, using the previous call's return value as its first argument. In other words, it does:
tmp := pred(zero, slc[len(slc)-1]) tmp = pred(tmp, slc[len(slc)-2]) tmp = pred(tmp, slc[len(slc)-3]) ... return tmp
func Foldr ¶
func Foldr(slc, zero, pred interface{}) interface{}
func foldl(slc []T, zero U, pred func(T, U) U) U
Foldr applies pred to each element of slc, using the previous call's return value as its second argument. In other words, it does:
tmp := pred(slc[0], zero) tmp = pred(slc[1], tmp) tmp = pred(slc[2], tmp) ... return tmp
func Identity ¶
func Identity(x interface{}) interface{}
func Identity(x T) T
Identity returns its argument.
func Map ¶
func Map(slc, pred interface{}) interface{}
func Map(slc []T, pred func(T) U) []U
Map applies pred to each element of slc successively, and returns the results.
func Max ¶
func Max(slc, less interface{}) interface{}
func Max(slc []T, less func(T, T) bool) T
Max finds the largest element in slc according to less (less(a, b) returns (a < b)). If len(slc) == 0, Max will return a nil interface, thus breaking the type assertion guarantee. However, so long as len(slc) > 0, the type assertion guarantee holds.
func Min ¶
func Min(slc, less interface{}) interface{}
func Min(slc []T, less func(T, T) bool) T
Min finds the smallest element in slc according to less (less(a, b) returns (a < b)). If len(slc) == 0, Min will return a nil interface, thus breaking the type assertion guarantee. However, so long as len(slc) > 0, the type assertion guarantee holds.
func Reject ¶
func Reject(slc, pred interface{}) interface{}
func Reject(slc []T, pred func(T) bool) []T
Reject applies pred to each element of slc, and returns those elements for which pred returned false. The returned slice will be only as long as it needs to to store all "false" elements, not necessarily as long as slc.
Types ¶
This section is empty.