A complex-number composite for Composition-Oriented Programming. A Complex
models Ruby's Complex (a + bi) over a Go complex128 value and exposes its
arithmetic as fallible operations that return a Result — so failures (the
canonical example being a division by 0+0i) are values, never panics and
never nil.
quotient := numerator.Div(denominator)
if quotient.HasError() {
fmt.Println(quotient.Error().Message()) // "division by zero"
} else {
fmt.Println(quotient.Payload().(Complex.Interface).ToGoString())
}Complex follows the org's Null-Object / never-nil invariant (enforced by the
nonnil CI analyzer): the NullComplex variant in src/null satisfies the same
Interface and reports IsNull() == true.
The package is named
Complex(capitalised) because the lowercasecomplexis a Go builtin function and cannot be used as a package name.
export GOPRIVATE=github.com/go-composites GOPROXY=direct GOSUMDB=off
go get github.com/go-composites/complex@main[!NOTE] main.go
package main
import (
"fmt"
Complex "github.com/go-composites/complex/src"
)
func main() {
a := Complex.New(1, 2) // 1+2i
b := Complex.New(3, 4) // 3+4i
// Arithmetic returns a Result.
product := a.Mul(b)
fmt.Println(product.Payload().(Complex.Interface).ToGoString()) // -5+10i
// Division by 0+0i is a value, not a NaN/Inf or a panic.
div := a.Div(Complex.New(0, 0))
fmt.Println("has error:", div.HasError()) // true
fmt.Println(div.Error().Message()) // division by zero
fmt.Println(b.Abs()) // 5
fmt.Println(b.Conjugate().ToGoString()) // 3-4i
}$ task buildConstructors
New(real, imag float64) Interface— builda + bifrom its parts.FromReal(r float64) Interface— a Complex with a zero imaginary part.null.New() Interface— theNullComplexNull-Object (IsNull() == true).
Accessors
Real() float64,Imaginary() float64.ToGoString() string—"a+bi"/"a-bi".IsNull() bool.
Arithmetic (each returns Result.Interface)
Add(other) Result/Sub(other) Result/Mul(other) Result.Div(other) Result— aResultcarryingError.New("division by zero")whenotheris0+0i.
Magnitude, conjugate and equality
Abs() float64— the magnitude (modulus) viamath/cmplx.Conjugate() Interface—a - bi.Equal(other) bool.
BSD-3-Clause — see LICENSE.
