Documentation
¶
Overview ¶
Package boolean provides functional programming utilities for working with boolean values.
This package offers algebraic structures (Monoid, Eq, Ord) for boolean values, enabling functional composition and reasoning about boolean operations.
Monoids ¶
The package provides two monoid instances for booleans:
- MonoidAny: Combines booleans using logical OR (disjunction), with false as identity
- MonoidAll: Combines booleans using logical AND (conjunction), with true as identity
MonoidAny - Logical OR ¶
MonoidAny implements the boolean monoid under disjunction (OR operation). The identity element is false, meaning false OR x = x for any boolean x.
Example:
import "github.com/IBM/fp-go/v2/boolean"
// Combine multiple booleans with OR
result := boolean.MonoidAny.Concat(false, true) // true
result2 := boolean.MonoidAny.Concat(false, false) // false
// Identity element
identity := boolean.MonoidAny.Empty() // false
// Check if any value in a collection is true
import "github.com/IBM/fp-go/v2/array"
values := []bool{false, false, true, false}
anyTrue := array.Fold(boolean.MonoidAny)(values) // true
MonoidAll - Logical AND ¶
MonoidAll implements the boolean monoid under conjunction (AND operation). The identity element is true, meaning true AND x = x for any boolean x.
Example:
import "github.com/IBM/fp-go/v2/boolean"
// Combine multiple booleans with AND
result := boolean.MonoidAll.Concat(true, true) // true
result2 := boolean.MonoidAll.Concat(true, false) // false
// Identity element
identity := boolean.MonoidAll.Empty() // true
// Check if all values in a collection are true
import "github.com/IBM/fp-go/v2/array"
values := []bool{true, true, true}
allTrue := array.Fold(boolean.MonoidAll)(values) // true
Equality ¶
The Eq instance provides structural equality for booleans:
import "github.com/IBM/fp-go/v2/boolean" equal := boolean.Eq.Equals(true, true) // true equal2 := boolean.Eq.Equals(true, false) // false
Ordering ¶
The Ord instance provides a total ordering for booleans where false < true:
import "github.com/IBM/fp-go/v2/boolean" cmp := boolean.Ord.Compare(false, true) // -1 (false < true) cmp2 := boolean.Ord.Compare(true, false) // +1 (true > false) cmp3 := boolean.Ord.Compare(true, true) // 0 (equal)
Use Cases ¶
The boolean package is particularly useful for:
- Combining multiple boolean conditions functionally
- Implementing validation logic that accumulates results
- Working with predicates in a composable way
- Folding collections of boolean values
Example - Validation:
import (
"github.com/IBM/fp-go/v2/array"
"github.com/IBM/fp-go/v2/boolean"
)
type User struct {
Name string
Email string
Age int
}
// Define validation predicates
validations := []func(User) bool{
func(u User) bool { return len(u.Name) > 0 },
func(u User) bool { return len(u.Email) > 0 },
func(u User) bool { return u.Age >= 18 },
}
// Check if user passes all validations
user := User{"Alice", "alice@example.com", 25}
results := array.Map(func(v func(User) bool) bool {
return v(user)
})(validations)
allValid := array.Fold(boolean.MonoidAll)(results) // true
Example - Any Match:
import (
"github.com/IBM/fp-go/v2/array"
"github.com/IBM/fp-go/v2/boolean"
)
// Check if any number is even
numbers := []int{1, 3, 5, 7, 8, 9}
checks := array.Map(func(n int) bool {
return n%2 == 0
})(numbers)
hasEven := array.Fold(boolean.MonoidAny)(checks) // true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // MonoidAny is the boolean [monoid.Monoid] under disjunction MonoidAny = monoid.MakeMonoid( func(l, r bool) bool { return l || r }, false, ) // MonoidAll is the boolean [monoid.Monoid] under conjuction MonoidAll = monoid.MakeMonoid( func(l, r bool) bool { return l && r }, true, ) // Eq is the equals predicate for boolean Eq = eq.FromStrictEquals[bool]() // Ord is the strict ordering for boolean Ord = ord.MakeOrd(func(l, r bool) int { if l { if r { return 0 } return +1 } if r { return -1 } return 0 }, func(l, r bool) bool { return l == r }) )
Functions ¶
func Fold ¶ added in v2.3.92
Fold converts a boolean into a value of type T by selecting between two thunks based on the boolean's value.
If the boolean is true, onTrue is evaluated and its result is returned. If the boolean is false, onFalse is evaluated and its result is returned.
The thunks are only evaluated when Fold is applied to a boolean value, making this safe to use with side-effectful or expensive computations.
Type Parameters:
- T: the type of value produced by either thunk
Parameters:
- onFalse: thunk evaluated when the boolean is false
- onTrue: thunk evaluated when the boolean is true
Returns:
- func(bool) T: a function that maps a boolean to a value of type T
Example (Int) ¶
ExampleFold_int demonstrates mapping a boolean to integer values.
score := Fold(lazy.Of(0), lazy.Of(100)) fmt.Println(score(true)) fmt.Println(score(false))
Output: 100 0
Example (Lazy) ¶
ExampleFold_lazy demonstrates that thunks are evaluated lazily — the unchosen branch is never called.
expensive := func() string {
// In real code this might be a costly computation; it is only
// called when the boolean is false.
return "fallback"
}
result := Fold(expensive, lazy.Of("fast-path"))(true)
fmt.Println(result)
Output: fast-path
Example (String) ¶
ExampleFold demonstrates selecting a string based on a boolean condition.
label := Fold(lazy.Of("inactive"), lazy.Of("active"))
fmt.Println(label(true))
fmt.Println(label(false))
Output: active inactive