github.com/enetx/g@v1.0.80/f/f.go (about) 1 package f 2 3 import ( 4 "cmp" 5 "reflect" 6 7 "github.com/enetx/g/pkg/constraints" 8 ) 9 10 // Comparable reports whether the value v is comparable. 11 func Comparable[T any](v T) bool { return reflect.ValueOf(v).Comparable() } 12 13 // Zero is a generic function designed to check if a value is considered zero. 14 func Zero[T cmp.Ordered](v T) bool { return v == *new(T) } 15 16 // Even is a generic function that checks if the provided integer is even. 17 func Even[T constraints.Integer](int T) bool { return int%2 == 0 } 18 19 // Odd is a generic function that checks if the provided integer is odd. 20 func Odd[T constraints.Integer](int T) bool { return int%2 != 0 } 21 22 // Eq returns a comparison function that evaluates to true when a value is equal to the provided threshold. 23 func Eq[T comparable](t T) func(T) bool { 24 return func(s T) bool { 25 return s == t 26 } 27 } 28 29 // Ne returns a comparison function that evaluates to true when a value is not equal to the provided threshold. 30 func Ne[T comparable](t T) func(T) bool { 31 return func(s T) bool { 32 return s != t 33 } 34 } 35 36 // Eqd returns a comparison function that evaluates to true when a value is deeply equal to the provided threshold. 37 func Eqd[T any](t T) func(T) bool { 38 return func(s T) bool { 39 return reflect.DeepEqual(t, s) 40 } 41 } 42 43 // Ned returns a comparison function that evaluates to true when a value is not deeply equal to the provided threshold. 44 func Ned[T any](t T) func(T) bool { 45 return func(s T) bool { 46 return !reflect.DeepEqual(t, s) 47 } 48 } 49 50 // Gt returns a comparison function that evaluates to true when a value is greater than the threshold. 51 func Gt[T cmp.Ordered](t T) func(T) bool { 52 return func(s T) bool { 53 return s > t 54 } 55 } 56 57 // Gte returns a comparison function that evaluates to true when a value is greater than or equal to the threshold. 58 func Gte[T cmp.Ordered](t T) func(T) bool { 59 return func(s T) bool { 60 return s >= t 61 } 62 } 63 64 // Lt returns a comparison function that evaluates to true when a value is less than the threshold. 65 func Lt[T cmp.Ordered](t T) func(T) bool { 66 return func(s T) bool { 67 return s < t 68 } 69 } 70 71 // Lte returns a comparison function that evaluates to true when a value is less than or equal to the threshold. 72 func Lte[T cmp.Ordered](t T) func(T) bool { 73 return func(s T) bool { 74 return s <= t 75 } 76 }