Skip to content

Commit

Permalink
Adds mathutil package (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenwallis authored Jul 29, 2022
1 parent 479e5ca commit a107145
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/aidenwallis/go-utils

go 1.18

require github.com/stretchr/testify v1.7.1
require (
github.com/stretchr/testify v1.7.1
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
12 changes: 12 additions & 0 deletions mathutil/clamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mathutil

// Clamp returns a value restricted between lo and hi.
func Clamp[T Number](v, lo, hi T) T {
if v < lo {
return lo
}
if v > hi {
return hi
}
return v
}
14 changes: 14 additions & 0 deletions mathutil/clamp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mathutil_test

import (
"testing"

"github.com/aidenwallis/go-utils/mathutil"
"github.com/stretchr/testify/assert"
)

func TestClamp(t *testing.T) {
assert.Equal(t, 20, mathutil.Clamp(1, 20, 100))
assert.Equal(t, 100, mathutil.Clamp(101, 20, 100))
assert.Equal(t, 35, mathutil.Clamp(35, 20, 100))
}
14 changes: 14 additions & 0 deletions mathutil/max.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mathutil

// Max finds the largest value of all given values
func Max[T Number](a T, rest ...T) T {
result := a

for _, v := range rest {
if v > result {
result = v
}
}

return result
}
15 changes: 15 additions & 0 deletions mathutil/max_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mathutil_test

import (
"testing"

"github.com/aidenwallis/go-utils/mathutil"
"github.com/stretchr/testify/assert"
)

func TestMax(t *testing.T) {
t.Parallel()
assert.Equal(t, 6, mathutil.Max(1, 2, 3, 4, 5, 6))
assert.Equal(t, 4, mathutil.Max(4))
assert.Equal(t, 7.0, mathutil.Max(4.0, 3.234, 1.0, 1.01, 5.0, 7.0, 2.0, 5.0, 5.0))
}
14 changes: 14 additions & 0 deletions mathutil/min.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mathutil

// Min finds the smallest value of all given values
func Min[T Number](a T, rest ...T) T {
result := a

for _, v := range rest {
if v < result {
result = v
}
}

return result
}
15 changes: 15 additions & 0 deletions mathutil/min_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mathutil_test

import (
"testing"

"github.com/aidenwallis/go-utils/mathutil"
"github.com/stretchr/testify/assert"
)

func TestMin(t *testing.T) {
t.Parallel()
assert.Equal(t, 1, mathutil.Min(1, 2, 3, 4, 5, 6))
assert.Equal(t, 4, mathutil.Min(4))
assert.Equal(t, 1.0, mathutil.Min(4.0, 3.234, 1.0, 1.01, 5.0, 7.0, 2.0, 5.0, 5.0))
}
8 changes: 8 additions & 0 deletions mathutil/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mathutil

import "golang.org/x/exp/constraints"

// Number is the type that supports any form of integer or float
type Number interface {
float32 | float64 | constraints.Integer
}

0 comments on commit a107145

Please sign in to comment.