github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/break/op/api.go (about) 1 // Package op provides generic operations that can be used for converting or reducing collections, loops, slices 2 package op 3 4 import ( 5 "golang.org/x/exp/constraints" 6 7 "github.com/m4gshm/gollections/c" 8 "github.com/m4gshm/gollections/op" 9 ) 10 11 // Sum returns the sum of two operands 12 func Sum[T c.Summable](a T, b T) (T, error) { 13 return op.Sum(a, b), nil 14 } 15 16 // Sub returns the substraction of the b from the a 17 func Sub[T c.Number](a T, b T) (T, error) { 18 return op.Sub(a, b), nil 19 } 20 21 // Max returns the maximum from two operands 22 func Max[T constraints.Ordered](a T, b T) (T, error) { 23 return IfElse(a < b, b, a) 24 } 25 26 // Min returns the minimum from two operands 27 func Min[T constraints.Ordered](a T, b T) (T, error) { 28 return IfElse(a > b, b, a) 29 } 30 31 // IfElse returns the tru value if ok, otherwise return the fal value 32 func IfElse[T any](ok bool, tru, fal T) (T, error) { 33 if ok { 34 return tru, nil 35 } 36 return fal, nil 37 } 38 39 // IfDoElse exececutes the tru func if ok, otherwise exec the fal function and returns it result 40 func IfDoElse[T any](ok bool, tru, fal func() (T, error)) (T, error) { 41 if ok { 42 return tru() 43 } 44 return fal() 45 }