predicate

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 0 Imported by: 1

README

Tests on Linux, MacOS and Windows Go Report Card GoDoc

var (
   pHello predicate.P[string] = func(s string) bool {
      return s == "hello"
   }
   pWorld predicate.P[string] = func(s string) bool {
      return s == "world"
   }
   pAny predicate.P[string] = func(s string) bool {
      return s != ""
   }
)

fmt.Println("Or (true):", pHello.Or(pWorld)("hello"))
fmt.Println("Or (false):", pHello.Or(pWorld)("foo"))
fmt.Println("And (false):", pHello.And(pWorld)("hello"))
fmt.Println("And (true):", pHello.And(pAny)("hello"))
fmt.Println("Negate (false):", pHello.Negate()("hello"))
fmt.Println("Negate (true):", pHello.Negate()("world"))
fmt.Println("Chained (true):", pHello.And(pAny.Or(pWorld))("hello"))
fmt.Println("Chained (false):", pHello.And(pAny.Or(pWorld))("foo"))

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type P

type P[T any] func(T) bool

P is a predicate function that tests whether a value of type T satisfies some condition.

Example
package main

import (
	"fmt"

	"github.com/bep/predicate"
)

func main() {
	var (
		pHello predicate.P[string] = func(s string) bool {
			return s == "hello"
		}
		pWorld predicate.P[string] = func(s string) bool {
			return s == "world"
		}
		pAny predicate.P[string] = func(s string) bool {
			return s != ""
		}
	)

	fmt.Println("Or (true):", pHello.Or(pWorld)("hello"))
	fmt.Println("Or (false):", pHello.Or(pWorld)("foo"))
	fmt.Println("And (false):", pHello.And(pWorld)("hello"))
	fmt.Println("And (true):", pHello.And(pAny)("hello"))
	fmt.Println("Negate (false):", pHello.Negate()("hello"))
	fmt.Println("Negate (true):", pHello.Negate()("world"))
	fmt.Println("Chained (true):", pHello.And(pAny.Or(pWorld))("hello"))
	fmt.Println("Chained (false):", pHello.And(pAny.Or(pWorld))("foo"))

}
Output:

Or (true): true
Or (false): false
And (false): false
And (true): true
Negate (false): false
Negate (true): true
Chained (true): true
Chained (false): false

func (P[T]) And

func (p P[T]) And(ps ...P[T]) P[T]

And returns a predicate that is a short-circuiting logical AND of this and the given predicates.

func (P[T]) Filter added in v0.2.0

func (p P[T]) Filter(s []T) []T

Filter returns a new slice holding only the elements of s that satisfy p. Filter modifies the contents of the slice s and returns the modified slice, which may have a smaller length.

func (P[T]) FilterCopy added in v0.2.0

func (p P[T]) FilterCopy(s []T) []T

FilterCopy returns a new slice holding only the elements of s that satisfy p.

func (P[T]) Negate

func (p P[T]) Negate() P[T]

Negate returns a predicate that is a logical negation of this predicate.

func (P[T]) Or

func (p P[T]) Or(ps ...P[T]) P[T]

Or returns a predicate that is a short-circuiting logical OR of this and the given predicates.

Jump to

Keyboard shortcuts

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