boolean

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

This section is empty.

Types

type Monoid

type Monoid = monoid.Monoid[bool]

Jump to

Keyboard shortcuts

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