v2 · Utilities
Predicate Boolean Logic
Predicates are functions that return boolean values. The predicate package provides utilities for combining and manipulating them.
Overview
A Predicate is a function that takes a value and returns a boolean. Predicates are fundamental for:
- Filtering collections
- Validation logic
- Conditional operations
- Boolean algebra
The predicate package provides combinators to compose complex predicates from simple ones.
Basic Predicates
predicate_basic.go
import P "github.com/IBM/fp-go/predicate"
// Define simple predicates
isEven := func(n int) bool {
return n%2 == 0
}
isPositive := func(n int) bool {
return n > 0
}
isGreaterThan10 := func(n int) bool {
return n > 10
}
// Use predicates
isEven(4) // true
isPositive(-5) // false
isGreaterThan10(15) // trueNot Combinator
Negate a predicate:
predicate_not.go
import P "github.com/IBM/fp-go/predicate"
isEven := func(n int) bool { return n%2 == 0 }
// Negate the predicate
isOdd := P.Not(isEven)
isOdd(3) // true
isOdd(4) // false
// Double negation
isEvenAgain := P.Not(isOdd)
isEvenAgain(4) // trueAnd Combinator
Combine predicates with logical AND:
predicate_and.go
import P "github.com/IBM/fp-go/predicate"
isEven := func(n int) bool { return n%2 == 0 }
isPositive := func(n int) bool { return n > 0 }
// Both conditions must be true
isEvenAndPositive := P.And(isEven, isPositive)
isEvenAndPositive(4) // true (even AND positive)
isEvenAndPositive(-2) // false (even but NOT positive)
isEvenAndPositive(3) // false (positive but NOT even)
isEvenAndPositive(-3) // false (neither)
// Chain multiple conditions
isGreaterThan10 := func(n int) bool { return n > 10 }
complexPredicate := P.And(
isEven,
P.And(isPositive, isGreaterThan10),
)
complexPredicate(12) // true
complexPredicate(8) // false (not > 10)Or Combinator
Combine predicates with logical OR:
predicate_or.go
import P "github.com/IBM/fp-go/predicate"
isEven := func(n int) bool { return n%2 == 0 }
isPositive := func(n int) bool { return n > 0 }
// At least one condition must be true
isEvenOrPositive := P.Or(isEven, isPositive)
isEvenOrPositive(4) // true (even OR positive - both)
isEvenOrPositive(-2) // true (even OR positive - even only)
isEvenOrPositive(3) // true (even OR positive - positive only)
isEvenOrPositive(-3) // false (neither)
// Complex combinations
isSpecial := P.Or(
P.And(isEven, isPositive),
func(n int) bool { return n == 0 },
)
isSpecial(4) // true (even and positive)
isSpecial(0) // true (special case)
isSpecial(-2) // falseValidation Example
Use predicates for complex validation:
predicate_validation.go
type User struct {
Name string
Age int
Email string
}
// Define validation predicates
hasName := func(u User) bool {
return u.Name != ""
}
isAdult := func(u User) bool {
return u.Age >= 18
}
hasEmail := func(u User) bool {
return u.Email != "" && strings.Contains(u.Email, "@")
}
// Combine validators
isValidUser := P.And(
hasName,
P.And(isAdult, hasEmail),
)
user1 := User{Name: "Alice", Age: 25, Email: "alice@example.com"}
user2 := User{Name: "Bob", Age: 16, Email: "bob@example.com"}
user3 := User{Name: "", Age: 30, Email: "test@example.com"}
isValidUser(user1) // true
isValidUser(user2) // false (not adult)
isValidUser(user3) // false (no name)Filtering with Predicates
Use predicates to filter collections:
predicate_filter.go
import (
A "github.com/IBM/fp-go/array"
F "github.com/IBM/fp-go/function"
P "github.com/IBM/fp-go/predicate"
)
numbers := []int{-2, -1, 0, 1, 2, 3, 4, 5, 6}
isEven := func(n int) bool { return n%2 == 0 }
isPositive := func(n int) bool { return n > 0 }
// Filter with combined predicates
evenAndPositive := F.Pipe2(
numbers,
A.Filter(P.And(isEven, isPositive)),
)
// []int{2, 4, 6}
// Filter with OR
evenOrPositive := F.Pipe2(
numbers,
A.Filter(P.Or(isEven, isPositive)),
)
// []int{-2, 0, 1, 2, 3, 4, 5, 6}
// Filter with NOT
notEven := F.Pipe2(
numbers,
A.Filter(P.Not(isEven)),
)
// []int{-1, 1, 3, 5}API Reference
| Function | Type | Description |
|---|---|---|
Not[A] | Predicate[A] -> Predicate[A] | Negates a predicate |
And[A] | (Predicate[A], Predicate[A]) -> Predicate[A] | Logical AND of two predicates |
Or[A] | (Predicate[A], Predicate[A]) -> Predicate[A] | Logical OR of two predicates |
Type Definition:
type Predicate[A any] func(A) bool
Related Concepts
Common Use Cases:
- Filtering arrays and collections
- Validation logic
- Conditional branching
- Boolean algebra operations
See Also:
- Function - Core function utilities
- Boolean - Boolean type class instances
- Array Filter - Using predicates with arrays