Documentation
¶
Overview ¶
Package number provides algebraic structures and utility functions for numeric types.
Overview ¶
This package defines common algebraic structures (Magma, Semigroup, Monoid) for numeric types, along with utility functions for arithmetic operations. It works with any type that satisfies the Number constraint (integers, floats, complex numbers).
The Number type constraint:
type Number interface {
constraints.Integer | constraints.Float | constraints.Complex
}
Algebraic Structures ¶
Magma - Binary operations (not necessarily associative):
- MagmaSub[A Number]() - Subtraction magma
- MagmaDiv[A Number]() - Division magma
Semigroup - Associative binary operations:
- SemigroupSum[A Number]() - Addition semigroup
- SemigroupProduct[A Number]() - Multiplication semigroup
Monoid - Semigroups with identity elements:
- MonoidSum[A Number]() - Addition monoid (identity: 0)
- MonoidProduct[A Number]() - Multiplication monoid (identity: 1)
Basic Usage ¶
Using monoids for numeric operations:
// Sum monoid sumMonoid := number.MonoidSum[int]() result := sumMonoid.Concat(5, 3) // 8 empty := sumMonoid.Empty() // 0 // Product monoid prodMonoid := number.MonoidProduct[int]() result := prodMonoid.Concat(5, 3) // 15 empty := prodMonoid.Empty() // 1
Using semigroups:
// Addition semigroup addSemigroup := number.SemigroupSum[int]() result := addSemigroup.Concat(10, 20) // 30 // Multiplication semigroup mulSemigroup := number.SemigroupProduct[float64]() result := mulSemigroup.Concat(2.5, 4.0) // 10.0
Using magmas (non-associative operations):
// Subtraction magma subMagma := number.MagmaSub[int]() result := subMagma.Concat(10, 3) // 7 // Division magma divMagma := number.MagmaDiv[float64]() result := divMagma.Concat(10.0, 2.0) // 5.0
Curried Arithmetic Functions ¶
The package provides curried versions of arithmetic operations for use in functional composition:
// Add - curried addition add5 := number.Add(5) result := add5(10) // 15 // Sub - curried subtraction sub3 := number.Sub(3) result := sub3(10) // 7 // Mul - curried multiplication double := number.Mul(2) result := double(5) // 10 // Div - curried division half := number.Div[float64](2) result := half(10.0) // 5.0
Using with array operations:
import (
A "github.com/IBM/fp-go/v2/array"
N "github.com/IBM/fp-go/v2/number"
)
numbers := []int{1, 2, 3, 4, 5}
// Add 10 to each number
result := A.Map(N.Add(10))(numbers)
// result: [11, 12, 13, 14, 15]
// Double each number
doubled := A.Map(N.Mul(2))(numbers)
// doubled: [2, 4, 6, 8, 10]
Utility Functions ¶
Inc - Increment a number:
result := number.Inc(5) // 6 result := number.Inc(2.5) // 3.5
Min - Get the minimum of two values:
result := number.Min(5, 3) // 3 result := number.Min(2.5, 7.8) // 2.5
Max - Get the maximum of two values:
result := number.Max(5, 3) // 5 result := number.Max(2.5, 7.8) // 7.8
Working with Different Numeric Types ¶
Integers:
sumMonoid := number.MonoidSum[int]() result := sumMonoid.Concat(100, 200) // 300 add10 := number.Add(10) result := add10(5) // 15
Floats:
sumMonoid := number.MonoidSum[float64]() result := sumMonoid.Concat(3.14, 2.86) // 6.0 half := number.Div[float64](2.0) result := half(10.5) // 5.25
Complex numbers:
sumMonoid := number.MonoidSum[complex128]() c1 := complex(1, 2) c2 := complex(3, 4) result := sumMonoid.Concat(c1, c2) // (4+6i)
Combining with Monoid Operations ¶
Using with monoid.ConcatAll:
import (
M "github.com/IBM/fp-go/v2/monoid"
N "github.com/IBM/fp-go/v2/number"
)
// Sum all numbers
sumMonoid := N.MonoidSum[int]()
numbers := []int{1, 2, 3, 4, 5}
total := M.ConcatAll(sumMonoid)(numbers)
// total: 15
// Product of all numbers
prodMonoid := N.MonoidProduct[int]()
product := M.ConcatAll(prodMonoid)(numbers)
// product: 120
Practical Examples ¶
Calculate average:
import (
M "github.com/IBM/fp-go/v2/monoid"
N "github.com/IBM/fp-go/v2/number"
)
numbers := []float64{10.0, 20.0, 30.0, 40.0, 50.0}
sumMonoid := N.MonoidSum[float64]()
sum := M.ConcatAll(sumMonoid)(numbers)
average := sum / float64(len(numbers))
// average: 30.0
Factorial using product monoid:
import (
M "github.com/IBM/fp-go/v2/monoid"
N "github.com/IBM/fp-go/v2/number"
)
factorial := func(n int) int {
if n <= 1 {
return 1
}
numbers := make([]int, n)
for i := range numbers {
numbers[i] = i + 1
}
prodMonoid := N.MonoidProduct[int]()
return M.ConcatAll(prodMonoid)(numbers)
}
result := factorial(5) // 120
Transform and sum:
import (
A "github.com/IBM/fp-go/v2/array"
F "github.com/IBM/fp-go/v2/function"
M "github.com/IBM/fp-go/v2/monoid"
N "github.com/IBM/fp-go/v2/number"
)
// Sum of squares
numbers := []int{1, 2, 3, 4, 5}
squares := A.Map(func(x int) int { return x * x })(numbers)
sumMonoid := N.MonoidSum[int]()
sumOfSquares := M.ConcatAll(sumMonoid)(squares)
// sumOfSquares: 55 (1 + 4 + 9 + 16 + 25)
Type Safety ¶
All functions are type-safe and work with any numeric type:
// Works with int intSum := number.MonoidSum[int]() // Works with float64 floatSum := number.MonoidSum[float64]() // Works with complex128 complexSum := number.MonoidSum[complex128]() // Compile-time error for non-numeric types // stringSum := number.MonoidSum[string]() // Error!
Functions ¶
Algebraic structures:
- MagmaSub[A Number]() - Subtraction magma
- MagmaDiv[A Number]() - Division magma
- SemigroupSum[A Number]() - Addition semigroup
- SemigroupProduct[A Number]() - Multiplication semigroup
- MonoidSum[A Number]() - Addition monoid (identity: 0)
- MonoidProduct[A Number]() - Multiplication monoid (identity: 1)
Curried arithmetic:
- Add[T Number](T) func(T) T - Curried addition
- Sub[T Number](T) func(T) T - Curried subtraction
- Mul[T Number](T) func(T) T - Curried multiplication
- Div[T Number](T) func(T) T - Curried division
Utilities:
- Inc[T Number](T) T - Increment by 1
- Min[A Ordered](A, A) A - Minimum of two values
- Max[A Ordered](A, A) A - Maximum of two values
Related Packages ¶
- magma: Base algebraic structure (binary operation)
- semigroup: Associative binary operation
- monoid: Semigroup with identity element
- constraints: Type constraints for generics
Package number provides utility functions for numeric operations with a functional programming approach. It includes curried arithmetic operations, comparison functions, and min/max utilities that work with generic numeric types.
Index ¶
- func Add[T Number](right T) func(T) T
- func Div[T Number](right T) func(T) T
- func Inc[T Number](value T) T
- func LessThan[A C.Ordered](a A) func(A) bool
- func MagmaDiv[A Number]() M.Magma[A]
- func MagmaSub[A Number]() M.Magma[A]
- func Max[A C.Ordered](a, b A) A
- func Min[A C.Ordered](a, b A) A
- func MonoidProduct[A Number]() M.Monoid[A]
- func MonoidSum[A Number]() M.Monoid[A]
- func MoreThan[A C.Ordered](a A) func(A) bool
- func Mul[T Number](right T) func(T) T
- func SemigroupProduct[A Number]() S.Semigroup[A]
- func SemigroupSum[A Number]() S.Semigroup[A]
- func Sub[T Number](right T) func(T) T
- type Number
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add[T Number](right T) func(T) T
Add is a curried function that adds two numbers. It takes a right operand and returns a function that takes a left operand, returning their sum (left + right).
This curried form is useful for partial application and function composition.
Example:
add5 := Add(5) result := add5(10) // returns 15
func Div ¶
func Div[T Number](right T) func(T) T
Div is a curried function that divides two numbers. It takes a right operand (divisor) and returns a function that takes a left operand (dividend), returning their quotient (left / right).
Note: Division by zero will cause a panic for integer types or return infinity for float types.
This curried form is useful for partial application and function composition.
Example:
halve := Div(2) result := halve(10) // returns 5
func Inc ¶
func Inc[T Number](value T) T
Inc increments a number by 1. It works with any numeric type that satisfies the Number constraint.
Example:
result := Inc(5) // returns 6
func LessThan ¶
LessThan is a curried comparison function that checks if a value is less than another. It takes a threshold value 'a' and returns a predicate function that checks if 'a' is greater than its argument, meaning the argument is less than 'a'.
This curried form is useful for creating reusable predicates and function composition.
Example:
lessThan10 := LessThan(10) result := lessThan10(5) // returns true (5 is less than 10) result := lessThan10(10) // returns false (10 is not less than 10) result := lessThan10(15) // returns false (15 is not less than 10)
func Max ¶
Max returns the maximum of two ordered values. If the values are considered equal, the first argument is returned.
This function works with any type that satisfies the Ordered constraint, including all numeric types and strings.
Example:
result := Max(5, 10) // returns 10 result := Max(3.14, 2.71) // returns 3.14
func Min ¶
Min returns the minimum of two ordered values. If the values are considered equal, the first argument is returned.
This function works with any type that satisfies the Ordered constraint, including all numeric types and strings.
Example:
result := Min(5, 10) // returns 5 result := Min(3.14, 2.71) // returns 2.71
func MonoidProduct ¶
MonoidProduct is the [Monoid] that multiplies elements with a one empty element
func MoreThan ¶
MoreThan is a curried comparison function that checks if a value is more than (greater than) another. It takes a threshold value 'a' and returns a predicate function that checks if 'a' is less than its argument, meaning the argument is more than 'a'.
This curried form is useful for creating reusable predicates and function composition.
Example:
moreThan10 := MoreThan(10) result := moreThan10(15) // returns true (15 is more than 10) result := moreThan10(10) // returns false (10 is not more than 10) result := moreThan10(5) // returns false (5 is not more than 10)
func Mul ¶
func Mul[T Number](right T) func(T) T
Mul is a curried function that multiplies two numbers. It takes a right operand and returns a function that takes a left operand, returning their product (left * right).
This curried form is useful for partial application and function composition.
Example:
double := Mul(2) result := double(10) // returns 20
func SemigroupProduct ¶
func SemigroupSum ¶
func Sub ¶
func Sub[T Number](right T) func(T) T
Sub is a curried function that subtracts two numbers. It takes a right operand and returns a function that takes a left operand, returning their difference (left - right).
This curried form is useful for partial application and function composition.
Example:
sub5 := Sub(5) result := sub5(10) // returns 5