Documentation
¶
Overview ¶
🛟 Package lambdas provides helper generic functions.
These functions are especially helpful in combination with other `genesis` packages.
Index ¶
- func Abs[T Number](a T) T
- func Default[T any](T) T
- func DefaultTo[T any](def T) func(val T, err error) T
- func Ensure(err error)
- func EqualTo[T comparable](a T) func(T) bool
- func IsDefault[T comparable](value T) bool
- func IsEmpty[T any](items []T) bool
- func IsNaN[T comparable](value T) bool
- func IsNil[T any](value *T) bool
- func IsNotDefault[T comparable](value T) bool
- func IsNotEmpty[T any](items []T) bool
- func IsNotNaN[T comparable](value T) bool
- func IsNotNil[T any](value *T) bool
- func IsNotZero[T Number](n T) bool
- func IsZero[T Number](n T) bool
- func LessThan[T constraints.Ordered](a T) func(T) bool
- func Max[T constraints.Ordered](a T, b T) T
- func Min[T constraints.Ordered](a T, b T) T
- func Must[T any](val T, err error) T
- func Not[T constraints.Ordered](f func(T) bool) func(T) bool
- func Safe[T any](val T, err error) T
- type Number
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
func Abs[T Number](a T) T
Abs returns positive value
Example ¶
package main import ( "fmt" "github.com/life4/genesis/lambdas" ) func main() { res := lambdas.Abs(-13) fmt.Println(res) }
Output: 13
func Default ¶
func Default[T any](T) T
Default returns the default value of the same type as the given value.
A few examples:
- 0 for int and float
- "" for string
- nil for slice
func DefaultTo ¶
DefaultTo wraps a function invicotaion and returns the specified default value if it returned an error.
Example ¶
package main import ( "fmt" "github.com/life4/genesis/lambdas" "github.com/life4/genesis/slices" ) func main() { res := lambdas.DefaultTo(13)(slices.Min([]int{})) fmt.Println(res) }
Output: 13
func Ensure ¶ added in v1.1.0
func Ensure(err error)
Ensure wraps a function invicotaion and panic if it returned an error. Compared to Must, in Ensure the called function only returns an error.
func EqualTo ¶
func EqualTo[T comparable](a T) func(T) bool
EqualTo returns lambda that checks if an item is equal to the given value.
func IsDefault ¶
func IsDefault[T comparable](value T) bool
IsDefault checks if the given value is the default for this type.
A few examples:
- 0 for int and float
- "" for string
- nil for slice
func IsNaN ¶
func IsNaN[T comparable](value T) bool
func IsNotDefault ¶
func IsNotDefault[T comparable](value T) bool
IsNotDefault checks if the given value is not the default for this type.
A few examples:
- 0 for int and float
- "" for string
- nil for slice
func IsNotEmpty ¶
Empty checks if the given slice is not empty
func IsNotNaN ¶
func IsNotNaN[T comparable](value T) bool
func LessThan ¶
func LessThan[T constraints.Ordered](a T) func(T) bool
LessThan returns lambda that checks if an item is less than the given value.
func Max ¶
func Max[T constraints.Ordered](a T, b T) T
Max returns maximal value
Example ¶
package main import ( "fmt" "github.com/life4/genesis/lambdas" ) func main() { res := lambdas.Max(10, 13) fmt.Println(res) }
Output: 13
func Min ¶
func Min[T constraints.Ordered](a T, b T) T
Min returns minimal value
Example ¶
package main import ( "fmt" "github.com/life4/genesis/lambdas" ) func main() { res := lambdas.Min(15, 13) fmt.Println(res) }
Output: 13
func Must ¶
Must wraps a function invicotaion and panic if it returned an error.
Example ¶
package main import ( "fmt" "github.com/life4/genesis/lambdas" "github.com/life4/genesis/slices" ) func main() { res := lambdas.Must(slices.Min([]int{42, 7, 13})) fmt.Println(res) }
Output: 7
func Not ¶
func Not[T constraints.Ordered](f func(T) bool) func(T) bool
Not negates the result of a lambda
Types ¶
type Number ¶
type Number interface { constraints.Integer | constraints.Float }