Skip to content

An almost useless utility for normalizing a numeric range, with a wrapping function useful for polar coordinates. It's exploring go 1.x code generation. It is a clone of a JavaScript project by James Talmage (https://github.com/jamestalmage/normalize-range).

License

zzwx/interval

Repository files navigation

https://github.com/zzwx/interval

Interval

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

Motivation

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.

Installation

go get -u github.com/zzwx/interval

Usage

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)
}

Go Playground

API

https://pkg.go.dev/github.com/zzwx/interval

License

Original JavaScript author: James Talmage

MIT © Anton Veretennikov

About

An almost useless utility for normalizing a numeric range, with a wrapping function useful for polar coordinates. It's exploring go 1.x code generation. It is a clone of a JavaScript project by James Talmage (https://github.com/jamestalmage/normalize-range).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages