Documentation
¶
Overview ¶
Package pred provides various generic predicate functions.
Index ¶
- func AllOf[E any](ps ...func(E) bool) func(E) bool
- func And[E any](p1, p2 func(E) bool) func(E) bool
- func AnyOf[E any](ps ...func(E) bool) func(E) bool
- func DeepEqual[E any](want E) func(E) bool
- func Equal[E comparable](want E) func(E) bool
- func EqualBy[E any](want E, compare cmp.Comparer[E]) func(E) bool
- func False[E any]() func(E) bool
- func GreaterThan[E constraint.Ordered](want E) func(E) bool
- func GreaterThanBy[E any](want E, compare cmp.Comparer[E]) func(E) bool
- func GreaterThanOrEqual[E constraint.Ordered](want E) func(E) bool
- func GreaterThanOrEqualBy[E any](want E, compare cmp.Comparer[E]) func(E) bool
- func In[E comparable](want ...E) func(E) bool
- func InBy[E any](compare cmp.Comparer[E], want ...E) func(E) bool
- func InSet[E comparable, F any](want map[E]F) func(E) bool
- func InSlice[E comparable](want []E) func(E) bool
- func InSliceBy[E any](want []E, compare cmp.Comparer[E]) func(E) bool
- func LessThan[E constraint.Ordered](want E) func(E) bool
- func LessThanBy[E any](want E, compare cmp.Comparer[E]) func(E) bool
- func LessThanOrEqual[E constraint.Ordered](want E) func(E) bool
- func LessThanOrEqualBy[E any](want E, compare cmp.Comparer[E]) func(E) bool
- func Nil[E any]() func(*E) bool
- func NoneOf[E any](ps ...func(E) bool) func(E) bool
- func Not[E any](p func(E) bool) func(E) bool
- func NotDeepEqual[E any](want E) func(E) bool
- func NotEqual[E comparable](want E) func(E) bool
- func NotEqualBy[E any](want E, compare cmp.Comparer[E]) func(E) bool
- func NotIn[E comparable](e ...E) func(E) bool
- func NotInBy[E any](compare cmp.Comparer[E], want ...E) func(E) bool
- func NotInSet[E comparable, F any](want map[E]F) func(E) bool
- func NotInSlice[E comparable](want []E) func(E) bool
- func NotInSliceBy[E any](want []E, compare cmp.Comparer[E]) func(E) bool
- func NotNil[E any]() func(*E) bool
- func NotRoughlyEqual[E constraint.Float](want, epsilon E) func(E) bool
- func NotZero[E comparable]() func(E) bool
- func OneOf[E any](ps ...func(E) bool) func(E) bool
- func Or[E any](p1, p2 func(E) bool) func(E) bool
- func RoughlyEqual[E constraint.Float](want, epsilon E) func(E) bool
- func StringContains(substr string) func(string) bool
- func StringContainsAny(chars string) func(string) bool
- func StringContainsFunc(f func(rune) bool) func(string) bool
- func StringContainsRune(r rune) func(string) bool
- func StringEqualFold(s string) func(string) bool
- func StringHasPrefix(prefix string) func(string) bool
- func StringHasSuffix(suffix string) func(string) bool
- func True[E any]() func(E) bool
- func Zero[E comparable]() func(E) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllOf ¶
AllOf returns a function that returns true if all the provided predicates return true. It returns false when:
- any of the provided predicates return false, or
- no predicates are provided.
Examples:
t := pred.True[any]() f := pred.False[any]() pred.AllOf() // false (no predicates provided) pred.AllOf(t, t) // true (both predicates match) pred.AllOf(t, f) // false (one predicate matches) pred.AllOf(f, f) // false (no predicates match)
func And ¶
And returns a function that returns true if both the provided predicates return true.
Examples:
t := pred.True[any]() f := pred.False[any]() pred.And(t, t) // evaluates to true pred.And(t, f) // evaluates to false pred.And(f, f) // evaluates to false
func AnyOf ¶
AnyOf returns a function that returns true if any of the provided predicates return true. It returns false when:
- none of the provided predicates return true, or
- no predicates are provided.
Examples:
t := pred.True[any]() f := pred.False[any]() pred.AnyOf() // false (no predicates provided) pred.AnyOf(t, t) // true (both predicates match) pred.AnyOf(t, f) // true (one predicate matches) pred.AnyOf(f, f) // false (no predicates match)
func DeepEqual ¶
DeepEqual returns a function that returns true if the provided value is equal to the provided want value. It uses the reflect.DeepEqual function to compare values.
Examples:
p := pred.DeepEqual(0) p(0) // true p(1) // false
func Equal ¶
func Equal[E comparable](want E) func(E) bool
Equal returns a function that returns true if the provided value is equal to the provided want value. It uses the == operator to compare values.
Examples:
p := pred.Equal(0) p(0) // true p(1) // false
func EqualBy ¶
EqualBy returns a function that returns true if the provided value is equal to the provided want value. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.EqualBy(0, cmp.Natural[int]) p(0) // true p(1) // false
func GreaterThan ¶
func GreaterThan[E constraint.Ordered](want E) func(E) bool
GreaterThan returns a function that returns true if the provided value is greater than the provided want value. It uses the > operator to compare values.
Examples:
p := pred.GreaterThan(0) p(-1) // false p(0) // false p(1) // true
func GreaterThanBy ¶
GreaterThanBy returns a function that returns true if the provided value is greater than the provided want value. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.GreaterThanBy(0, cmp.Natural[int]) p(-1) // false p(0) // false p(1) // true
func GreaterThanOrEqual ¶
func GreaterThanOrEqual[E constraint.Ordered](want E) func(E) bool
GreaterThanOrEqual returns a function that returns true if the provided value is greater than or equal to the provided want value. It uses the >= operator to compare values.
Examples:
p := pred.GreaterThanOrEqual(0) p(-1) // false p(0) // true p(1) // true
func GreaterThanOrEqualBy ¶
GreaterThanOrEqualBy returns a function that returns true if the provided value is greater than or equal to the provided want value. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.GreaterThanOrEqualBy(0, cmp.Natural[int]) p(-1) // false p(0) // true p(1) // true
func In ¶
func In[E comparable](want ...E) func(E) bool
In returns a function that returns true if the provided value is equal to any of the provided want values. It uses the == operator to compare values.
Examples:
p := pred.In(0, 1, 2) p(0) // true p(1) // true p(2) // true p(3) // false
func InBy ¶
InBy returns a function that returns true if the provided value is equal to any of the provided want values. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.IntBy(cmp.Natural[int], 0, 1, 2) p(0) // true p(1) // true p(2) // true p(3) // false
func InSet ¶
func InSet[E comparable, F any](want map[E]F) func(E) bool
InSet returns a function that returns true if the provided value is found in the provided want set. The set is represented with map keys; the map values are ignored.
Examples:
set := map[int]struct{}{0: {}, 1: {}, 2: {}} p := pred.InSet(set) p(0) // true p(1) // true p(2) // true p(3) // false
func InSlice ¶
func InSlice[E comparable](want []E) func(E) bool
InSlice returns a function that returns true if the provided value is equal to any element in the provided want slice. It uses the == operator to compare values.
Examples:
p := pred.InSlice([]int{0, 1, 2}) p(0) // true p(1) // true p(2) // true p(3) // false
func InSliceBy ¶
InSliceBy returns a function that returns true if the provided value is equal to any element in the provided want slice. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.InSliceBy([]int{0, 1, 2}, cmp.Natural[int]) p(0) // true p(1) // true p(2) // true p(3) // false
func LessThan ¶
func LessThan[E constraint.Ordered](want E) func(E) bool
LessThan returns a function that returns true if the provided value is less than the provided want value. It uses the < operator to compare values.
Examples:
p := pred.LessThan(0) p(-1) // true p(0) // false p(1) // false
func LessThanBy ¶
LessThanBy returns a function that returns true if the provided value is less than the provided want value. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.LessThanBy(0, cmp.Natural[int]) p(-1) // true p(0) // false p(1) // false
func LessThanOrEqual ¶
func LessThanOrEqual[E constraint.Ordered](want E) func(E) bool
LessThanOrEqual returns a function that returns true if the provided value is less than or equal to the provided want value. It uses the <= operator to compare values.
Examples:
p := pred.LessThanOrEqual(0) p(-1) // true p(0) // true p(1) // false
func LessThanOrEqualBy ¶
LessThanOrEqualBy returns a function that returns true if the provided value is less than or equal to the provided want value. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.LessThanOrEqualBy(0, cmp.Natural[int]) p(-1) // true p(0) // true p(1) // false
func Nil ¶
Nil returns a function that returns true if the provided ptr is nil.
Examples:
p := pred.Nil[int]() p(nil) // true p(0) // false
func NoneOf ¶
NoneOf returns a function that returns true if none of the provided predicates return true. It returns false when any of the provided predicates return true. If no predicates are provided, it returns true (degenerate case).
Examples:
t := pred.True[any]() f := pred.False[any]() pred.NoneOf() // true (no predicates provided) pred.NoneOf(t, t) // false (both predicates match) pred.NoneOf(t, f) // false (one predicate matches) pred.NoneOf(f, f) // true (no predicates match)
func Not ¶
Not returns a function that returns the opposite of the provided predicate.
Examples:
t := pred.True[any]() f := pred.False[any]() pred.Not(t) // evaluates to false pred.Not(f) // evaluates to true
func NotDeepEqual ¶
NotDeepEqual returns a function that returns true if the provided value is not equal to the provided want value. It uses the reflect.DeepEqual function to compare values.
Examples:
p := pred.NotDeepEqual(0) p(0) // false p(1) // true
func NotEqual ¶
func NotEqual[E comparable](want E) func(E) bool
NotEqual returns a function that returns true if the provided value is not equal to the provided want value. It uses the != operator to compare values.
Examples:
p := pred.NotEqual(0) p(0) // false p(1) // true
func NotEqualBy ¶
NotEqualBy returns a function that returns true if the provided value is not equal to the provided want value. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.NotEqualBy(0, cmp.Natural[int]) p(0) // false p(1) // true
func NotIn ¶
func NotIn[E comparable](e ...E) func(E) bool
NotIn returns a function that returns true if the provided value is not equal to any of the provided want values. It uses the == operator to compare values.
Examples:
p := pred.NotIn(0, 1, 2) p(0) // false p(1) // false p(2) // false p(3) // true
func NotInBy ¶
NotInBy returns a function that returns true if the provided value is not equal to any of the provided want values. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.NotInBy(cmp.Natural[int], 0, 1, 2) p(0) // false p(1) // false p(2) // false p(3) // true
func NotInSet ¶
func NotInSet[E comparable, F any](want map[E]F) func(E) bool
NotInSet returns a function that returns true if the provided value is not found in the provided want set. The set is represented with map keys; the map values are ignored.
Examples:
set := map[int]struct{}{0: {}, 1: {}, 2: {}} p := pred.NotInSet(set) p(0) // false p(1) // false p(2) // false p(3) // true
func NotInSlice ¶
func NotInSlice[E comparable](want []E) func(E) bool
NotInSlice returns a function that returns true if the provided value is not equal to any element in the provided want slice. It uses the == operator to compare values.
Examples:
p := pred.NotInSlice([]int{0, 1, 2}) p(0) // false p(1) // false p(2) // false p(3) // true
func NotInSliceBy ¶
NotInSliceBy returns a function that returns true if the provided value is not equal to any element in the provided want slice. It uses the provided cmp.Comparer to compare values.
Examples:
p := pred.NotInSliceBy([]int{0, 1, 2}, cmp.Natural[int]) p(0) // false p(1) // false p(2) // false p(3) // true
func NotNil ¶
NotNil returns a function that returns true if the provided ptr is not nil.
Examples:
p := pred.NotNil[int]() p(nil) // false p(0) // true
func NotRoughlyEqual ¶
func NotRoughlyEqual[E constraint.Float](want, epsilon E) func(E) bool
NotRoughlyEqual returns a function specialized for floats that returns true if the provided value is not roughly equal to the provided want value relative to the provided epsilon.
Note: this function is symmetric, but less meaningful for values near or smaller than epsilon.
func NotZero ¶
func NotZero[E comparable]() func(E) bool
NotZero returns a function that returns true if the provided value is not the zero value of the type parameter E.
Examples:
p := pred.NotZero[int]() p(0) // false p(1) // true
func OneOf ¶
OneOf returns a function that returns true if exactly one of the provided predicates returns true. It returns false when:
- none of the provided predicates return true, or
- more than one of the provided predicates return true or
- no predicates are provided (degenerate case).
Examples:
t := pred.True[any]() f := pred.False[any]() pred.OneOf() // false (no predicates provided) pred.OneOf(t, t) // false (both predicates match) pred.OneOf(t, f) // true (one predicate matches) pred.OneOf(f, f) // false (no predicates match)
func Or ¶
Or returns a function that returns true if either the provided predicates return true.
Examples:
t := pred.True[any]() f := pred.False[any]() pred.Or(t, t) // evaluates to true pred.Or(t, f) // evaluates to true pred.Or(f, f) // evaluates to false
func RoughlyEqual ¶
func RoughlyEqual[E constraint.Float](want, epsilon E) func(E) bool
RoughlyEqual returns a function specialized for floats that returns true if the provided value is roughly equal to the provided want value relative to the provided epsilon.
Note: this function is symmetric, but less meaningful for values near or smaller than epsilon.
func StringContains ¶
StringContains returns a function that returns true if the provided string contains the provided substring. It uses the strings.Contains function to compare strings.
Examples:
p := pred.StringContains("foobar") p("foo") // true p("bar") // true p("baz") // false
func StringContainsAny ¶
StringContainsAny returns a function that returns true if the provided string contains any of the provided chars. It uses the strings.ContainsAny function to compare strings.
Examples:
p := pred.StringContainsAny("abc") p("foo") // false p("bar") // true p("baz") // true
func StringContainsFunc ¶
StringContainsFunc returns a function that returns true if the provided string contains a rune that satisfies the provided function. It uses the strings.ContainsFunc function to compare strings.
Examples:
p := pred.StringContainsFunc(func(r rune) bool { return r == 'a' }) p("foo") // false p("bar") // true p("baz") // true
func StringContainsRune ¶
StringContainsRune returns a function that returns true if the provided string contains the provided rune. It uses the strings.ContainsRune function to compare strings.
Examples:
p := pred.StringContainsRune('a') p("foo") // false p("bar") // true p("baz") // true
func StringEqualFold ¶
StringEqualFold returns a function that returns true if the provided string is equal to the provided want string, ignoring case. It uses the strings.EqualFold function to compare strings.
Examples:
p := pred.StringEqualFold("foo") p("foo") // true p("FOO") // true p("bar") // false
func StringHasPrefix ¶
StringHasPrefix returns a function that returns true if the provided string has the provided prefix. It uses the strings.HasPrefix function to compare strings.
Examples:
p := pred.StringHasPrefix("foo") p("foobar") // true p("fuzbar") // false
func StringHasSuffix ¶
StringHasSuffix returns a function that returns true if the provided string has the provided suffix. It uses the strings.HasSuffix function to compare strings.
Examples:
p := pred.StringHasSuffix("bar") p("foobar") // true p("foobaz") // false
func Zero ¶
func Zero[E comparable]() func(E) bool
Zero returns a function that returns true if the provided value is the zero value of the type parameter E.
Examples:
p := pred.Zero[int]() p(0) // true p(1) // false
Types ¶
This section is empty.