pred

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package pred provides various generic predicate functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllOf

func AllOf[E any](ps ...func(E) bool) func(E) bool

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

func And[E any](p1, p2 func(E) bool) func(E) bool

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

func AnyOf[E any](ps ...func(E) bool) func(E) bool

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

func DeepEqual[E any](want E) func(E) bool

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

func EqualBy[E any](want E, compare cmp.Comparer[E]) func(E) bool

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 False

func False[E any]() func(E) bool

False returns a function that always returns 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

func GreaterThanBy[E any](want E, compare cmp.Comparer[E]) func(E) bool

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

func GreaterThanOrEqualBy[E any](want E, compare cmp.Comparer[E]) func(E) bool

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

func InBy[E any](compare cmp.Comparer[E], want ...E) func(E) bool

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

func InSliceBy[E any](want []E, compare cmp.Comparer[E]) func(E) bool

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

func LessThanBy[E any](want E, compare cmp.Comparer[E]) func(E) bool

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

func LessThanOrEqualBy[E any](want E, compare cmp.Comparer[E]) func(E) bool

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

func Nil[E any]() func(*E) bool

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

func NoneOf[E any](ps ...func(E) bool) func(E) bool

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

func Not[E any](p func(E) bool) func(E) bool

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

func NotDeepEqual[E any](want E) func(E) bool

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

func NotEqualBy[E any](want E, compare cmp.Comparer[E]) func(E) bool

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

func NotInBy[E any](compare cmp.Comparer[E], want ...E) func(E) bool

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

func NotInSliceBy[E any](want []E, compare cmp.Comparer[E]) func(E) bool

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

func NotNil[E any]() func(*E) bool

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

func OneOf[E any](ps ...func(E) bool) func(E) bool

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

func Or[E any](p1, p2 func(E) bool) func(E) bool

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

func StringContains(substr string) func(string) bool

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

func StringContainsAny(chars string) func(string) bool

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

func StringContainsFunc(f func(rune) bool) func(string) bool

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

func StringContainsRune(r rune) func(string) bool

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

func StringEqualFold(s string) func(string) bool

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

func StringHasPrefix(prefix string) func(string) bool

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

func StringHasSuffix(suffix string) func(string) bool

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 True

func True[E any]() func(E) bool

True returns a function that always returns true.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL