An almost useless utility for normalizing a numeric range, with a wrapping function for polar coordinates, implemented using go generate
.
It is a golang clone of a JavaScript project by James Talmage.
For dealing with the strict typing in Go, functions were auto-generated for all of the following types:
int
int64
int32
int16
int8
uint
uint64
uint32
uint16
uint8
float32
float64
This approach is inspired by Rob Pike's article on code generation. Until generics are implemented, this is simply an example of code generation, exploiting templates in this case.
go get -u github.com/zzwx/interval
package main
import (
"fmt"
"github.com/zzwx/interval"
)
func main() {
fmt.Println(interval.WrapInt(0, 360, 400)) //=> 40
fmt.Println(interval.WrapInt(0, 360, -90)) //=> 270
fmt.Println(interval.ClampInt(0, 100, 500)) //=> 100
fmt.Println(interval.ClampInt(0, 100, -20)) //=> 0
r := interval.NewRangeFloat64(0, 100, false, false)
fmt.Println(r.Wrap(120)) //=> 20
fmt.Println(r.Validate(120)) //=> 0, error(120 is outside of range [0,100])
fmt.Println(r.Test(120)) //=> false
fmt.Println(r) //=> [0,100] (uses Stringer interface)
}
https://pkg.go.dev/github.com/zzwx/interval
Original JavaScript author: James Talmage
MIT © Anton Veretennikov