A numeric Range composite for Composition-Oriented Programming, modelled
on Ruby's Range. A Range is an integer interval with a non-zero step that
supports both an inclusive end (Ruby's 1..5) and an exclusive end
(Ruby's 1...5). Construction is fallible and returns a Result — an
invalid step (zero) is a value, never a panic and never nil.
The elements of a Range are exposed as go-composites Numbers:
inclusive := Range.New(1, 5).Payload().(Range.Interface)
inclusive.Each(func(n Number.Interface) Result.Interface {
fmt.Print(" ", n.ToGoString()) // 1 2 3 4 5
return Result.New()
})Range is deliberately integer-only (its bounds and step are Go
int64), which keeps element enumeration and 100% coverage tractable.
Range follows the org's Null-Object / never-nil invariant (enforced by the
nonnil CI analyzer): the NullRange variant in src/null satisfies the same
Interface, is an empty range, and reports IsNull() == true.
export GOPRIVATE=github.com/go-composites GOPROXY=direct GOSUMDB=off
go get github.com/go-composites/range@main[!NOTE] main.go
package main
import (
"fmt"
Number "github.com/go-composites/number/src"
Range "github.com/go-composites/range/src"
Result "github.com/go-composites/result/src"
)
func main() {
inclusive := Range.New(1, 5).Payload().(Range.Interface)
fmt.Println("1..5 length :", inclusive.Len()) // 5
fmt.Println("1..5 has 5 :", inclusive.Includes(5)) // true
exclusive := Range.New(1, 5, Range.Exclusive()).Payload().(Range.Interface)
fmt.Println("1...5 has 5 :", exclusive.Includes(5)) // false
bad := Range.New(0, 10, Range.WithStep(0))
fmt.Println("step 0 error:", bad.HasError()) // true
fmt.Println(bad.Error().Message()) // step cannot be zero
}$ task buildConstructor
New(begin, end int64, options ...Option) Result.Interface— payload is aRange, or anError("step cannot be zero") when the step is zero.WithStep(int64) Option— set the (non-zero) step (default1).Exclusive() Option— exclude the end bound (Ruby's1...5).null.New() Interface— theNullRangeNull-Object (IsNull() == true).
Bounds (each returns Number.Interface)
Begin(),End(),Step().ExcludesEnd() bool— whether the end bound is excluded.
Membership and size
Includes(n int64) bool— honours the step and the inclusive / exclusive end.Len() int,IsEmpty() bool.
Iteration and materialisation
Each(fn func(Number.Interface) Result.Interface) Result.Interface— short-circuits on the first errorResult.ToArray() Array.Interface— materialise the elements asNumbers.
Inspection
IsNull() bool.
BSD-3-Clause — see LICENSE.
