Documentation ¶
Overview ¶
Package gofuncs provides functions for working with functions of desired signatures SPDX-License-Identifier: Apache-2.0
Index ¶
- Variables
- func And(fns ...interface{}) func(interface{}) bool
- func Consumer(fn interface{}) func(interface{})
- func ConvertTo(out interface{}) func(interface{}) interface{}
- func DeepEqualTo(val interface{}) func(interface{}) bool
- func EqualTo(val interface{}) func(interface{}) bool
- func Filter(fn interface{}) func(interface{}) bool
- func FilterAll(fns ...interface{}) []func(interface{}) bool
- func GreaterThan(val interface{}) func(val1, val2 interface{}) bool
- func GreaterThanEquals(val interface{}) func(val1, val2 interface{}) bool
- func IndexOf(arrslc interface{}, index uint, defalt ...interface{}) interface{}
- func IsGreaterThan(val interface{}) func(interface{}) bool
- func IsGreaterThanEquals(val interface{}) func(interface{}) bool
- func IsLessThan(val interface{}) func(interface{}) bool
- func IsLessThanEquals(val interface{}) func(interface{}) bool
- func IsLessableKind(kind reflect.Kind) bool
- func IsNegative(val interface{}) bool
- func IsNil(val interface{}) bool
- func IsNilable(val interface{}) bool
- func IsNonNegative(val interface{}) bool
- func IsPositive(val interface{}) bool
- func LessThan(val interface{}) func(val1, val2 interface{}) bool
- func LessThanEquals(val interface{}) func(val1, val2 interface{}) bool
- func Map(fn interface{}) func(interface{}) interface{}
- func MapTo(fn interface{}, val interface{}) interface{}
- func Not(fn interface{}) func(interface{}) bool
- func Or(fns ...interface{}) func(interface{}) bool
- func PanicBM(valid bool, msg string)
- func PanicE(err error)
- func PanicVBM(val interface{}, valid bool, msg string) interface{}
- func PanicVE(val interface{}, err error) interface{}
- func SortFunc(fn interface{}) func(val1, val2 interface{}) bool
- func Supplier(fn interface{}) func() interface{}
- func SupplierOf(fn interface{}, val interface{}) interface{}
- func Ternary(expr bool, trueVal, falseVal interface{}) interface{}
- func TernaryOf(expr bool, trueVal, falseVal interface{}) interface{}
- func ValueOfKey(mp interface{}, key interface{}, defalt ...interface{}) interface{}
Constants ¶
This section is empty.
Variables ¶
var ( // IntSortFunc returns true if int64 val1 < val2 IntSortFunc = SortFunc(func(val1, val2 int64) bool { return val1 < val2 }) // UintSortFunc returns true if uint64 val1 < val2 UintSortFunc = SortFunc(func(val1, val2 uint64) bool { return val1 < val2 }) // FloatSortFunc returns true if float64 val1 < val2 FloatSortFunc = SortFunc(func(val1, val2 float64) bool { return val1 < val2 }) // ComplexSortFunc returns true if abs(complex128 val1) < abs(complex128 val2) ComplexSortFunc = SortFunc(func(val1, val2 complex128) bool { return cmplx.Abs(val1) < cmplx.Abs(val2) }) // StringSortFunc returns true if string val1 < val2 StringSortFunc = SortFunc(func(val1, val2 string) bool { return val1 < val2 }) // BigIntSortFunc returns true if big.Int val1 < val2 BigIntSortFunc = SortFunc(func(val1, val2 *big.Int) bool { return val1.Cmp(val2) == -1 }) // BigRatSortFunc returns true if big.Rat val1 < val2 BigRatSortFunc = SortFunc(func(val1, val2 *big.Rat) bool { return val1.Cmp(val2) == -1 }) // BigFloatSortFunc returns true if big.Float val1 < val2 BigFloatSortFunc = SortFunc(func(val1, val2 *big.Float) bool { return val1.Cmp(val2) == -1 }) )
Functions ¶
func And ¶
func And(fns ...interface{}) func(interface{}) bool
And (fns) any number of func(any)bool into the conjunction of all the funcs. Short-circuit logic will return false on the first function that returns false.
func Consumer ¶
func Consumer(fn interface{}) func(interface{})
Consumer (fn) adapts a func(any) into a func(interface{}) If fn happens to be a func(interface{}), it is returned as is. Otherwise, each invocation converts the arg passed to the type the func receives.
func ConvertTo ¶
func ConvertTo(out interface{}) func(interface{}) interface{}
ConvertTo generates a func(interface{}) interface{} that converts a value into the same type as the value passed. Eg, ConvertTo(int8(0)) converts a func that converts a value into an int8.
func DeepEqualTo ¶
func DeepEqualTo(val interface{}) func(interface{}) bool
DeepEqualTo (val) returns a func(interface{}) bool that returns true if the func arg is deep equal to val. The arg is converted to the type of val first, then compared. If val is nil, then the arg type must be convertible to the type of val. If val is an untyped nil, then the arg must be an untyped nil. Comparison is made using reflect.DeepEqual.
func EqualTo ¶
func EqualTo(val interface{}) func(interface{}) bool
EqualTo (val) returns a func(interface{}) bool that returns true if the func arg is equal to val. The arg is converted to the type of val first, then compared. If val is nil, then the arg type must be convertible to the type of val. If val is an untyped nil, then the arg must be an untyped nil. Comparison is made using == operator. If val is not comparable using == (eg, slices are not comparable), the result will be true if val and arg have the same address.
func Filter ¶
func Filter(fn interface{}) func(interface{}) bool
Filter (fn) adapts a func(any) bool into a func(interface{}) bool. If fn happens to be a func(interface{}) bool, it is returned as is. Otherwise, each invocation converts the arg passed to the type the func receives.
func FilterAll ¶
func FilterAll(fns ...interface{}) []func(interface{}) bool
FilterAll (fns) adapts any number of func(any) bool into a slice of func(interface{}) bool. Each func passed is separately adapted using Filter into the corresponding slice element of the result. FIlterAll is the basis for composing multiple logic functions into a single logic function. Note that when calling the provided set of logic functions, the argument type must be compatible with all of them. The most likely failure case is mixing funcs that accept interface{} that type assert the argument with funcs that accept a specific type.
func GreaterThan ¶
func GreaterThan(val interface{}) func(val1, val2 interface{}) bool
GreaterThan (val) returns a func(val1, val2 interface{}) bool that returns true if val1 > val2. The args are converted to the type of val first, then compared. Panics if val is nil or IsLessableKind(kind of val) is false.
func GreaterThanEquals ¶
func GreaterThanEquals(val interface{}) func(val1, val2 interface{}) bool
GreaterThanEquals (val) returns a func(val1, val2 interface{}) bool that returns true if val1 >= val2. The args are converted to the type of val first, then compared. Panics if val is nil or IsLessableKind(kind of val) is false.
func IndexOf ¶
func IndexOf(arrslc interface{}, index uint, defalt ...interface{}) interface{}
IndexOf returns the first of the following given an array or slice, index, and optional default value: 1. slice[index] if the array or slice length > index 2. default value if provided, converted to array or slice element type 3. zero value of array or slice element type Panics if arrslc is not an array or slice. Panics if the default value is not convertible to the array or slice element type, even if it is not needed.
func IsGreaterThan ¶
func IsGreaterThan(val interface{}) func(interface{}) bool
IsGreaterThan returns a func(arg interface{}) bool that returns true if arg > val
func IsGreaterThanEquals ¶
func IsGreaterThanEquals(val interface{}) func(interface{}) bool
IsGreaterThanEquals returns a func(arg interface{}) bool that returns true if arg >= val
func IsLessThan ¶
func IsLessThan(val interface{}) func(interface{}) bool
IsLessThan returns a func(arg interface{}) bool that returns true if arg < val
func IsLessThanEquals ¶
func IsLessThanEquals(val interface{}) func(interface{}) bool
IsLessThanEquals returns a func(arg interface{}) bool that returns true if arg <= val
func IsLessableKind ¶
IsLessableKind returns if if kind represents any numeric type or string
func IsNegative ¶
func IsNegative(val interface{}) bool
IsNegative (val) returns true if the val < 0
func IsNil ¶
func IsNil(val interface{}) bool
IsNil is a func(interface{}) bool that returns true if val is nil
func IsNilable ¶
func IsNilable(val interface{}) bool
IsNilable is a func(interface{}) bool that returns true if val is nil or the type of val is a nilable type. Returns true of the reflect.Kind of val is Chan, Func, Interface, Map, Ptr, or Slice.
func IsNonNegative ¶
func IsNonNegative(val interface{}) bool
IsNonNegative (val) returns true if val >= 0
func LessThan ¶
func LessThan(val interface{}) func(val1, val2 interface{}) bool
LessThan (val) returns a func(val1, val2 interface{}) bool that returns true if val1 < val2. The args are converted to the type of val first, then compared. Panics if val is nil or IsLessableKind(kind of val) is false.
func LessThanEquals ¶
func LessThanEquals(val interface{}) func(val1, val2 interface{}) bool
LessThanEquals (val) returns a func(val1, val2 interface{}) bool that returns true if val1 <= val2. The args are converted to the type of val first, then compared. Panics if val is nil or IsLessableKind(kind of val) is false.
func Map ¶
func Map(fn interface{}) func(interface{}) interface{}
Map (fn) adapts a func(any) any into a func(interface{}) interface{}. If fn happens to be a func(interface{}) interface{}, it is returned as is. Otherwise, each invocation converts the arg passed to the type the func receives.
func MapTo ¶
func MapTo(fn interface{}, val interface{}) interface{}
MapTo (fn, X) adapts a func(any) X' into a func(interface{}) X. If fn happens to be a func(interface{}) X, it is returned as is. Otherwise, each invocation converts the arg passed to the type the func receives, and type X' must be convertible to X. The result will have to be type asserted by the caller.
func Not ¶
func Not(fn interface{}) func(interface{}) bool
Not (fn) adapts a func(any) bool to the negation of the func.
func Or ¶
func Or(fns ...interface{}) func(interface{}) bool
Or (fns) any number of func(any)bool into the disjunction of all the funcs. Short-circuit logic will return true on the first function that returns true.
func PanicVE ¶
func PanicVE(val interface{}, err error) interface{}
PanicVE panics if err is non-nil, otherwise returns val
func SortFunc ¶
func SortFunc(fn interface{}) func(val1, val2 interface{}) bool
SortFunc adapts a func(val1, val2 any) bool into a func(val1, val2 interface{}) bool. If fn is already a func(val1, val2 interface{}) bool, it is returned as is. The passed func must return true if and only if val1 < val2. Panics if fn is nil, not a func, does not accept two args of the same type, or does not return a single bool value.
func Supplier ¶
func Supplier(fn interface{}) func() interface{}
Supplier (fn) adapts a func() any into a func() interface{}. If fn happens to be a func() interface{}, it is returned as is. fn may have a single variadic argument.
func SupplierOf ¶
func SupplierOf(fn interface{}, val interface{}) interface{}
SupplierOf (fn, X) adapts a func() X' into a func() X. If fn happens to be a func() X, it is returned as is. Otherwise, type X' must be convertible to X. The result will have to be type asserted by the caller. fn may have a single variadic argument.
func Ternary ¶
func Ternary(expr bool, trueVal, falseVal interface{}) interface{}
Ternary returns trueVal if expr is true, else it returns falseVal
func TernaryOf ¶
func TernaryOf(expr bool, trueVal, falseVal interface{}) interface{}
TernaryOf returns trueVal() if expr is true, else it returns falseVal() trueVal and falseVal must be func() any.
func ValueOfKey ¶
func ValueOfKey(mp interface{}, key interface{}, defalt ...interface{}) interface{}
ValueOfKey returns the first of the following: 1. map[key] if the key exists in the map 2. default if provided 3. zero value of map value type Panics if mp is not a map. Panics if the default value is not convertible to map value type, even if it is not needed.
Types ¶
This section is empty.