feat: math: add generics versions of Max, Min to cater to all numeric types (#14166)
This commit is contained in:
parent
1d2de854d1
commit
d301d15ed7
@ -34,3 +34,4 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
* [#14010](https://github.com/cosmos/cosmos-sdk/pull/14010) Optimize FormatInt to not do plain string concentation when formatting thousands and instead build it more efficiently.
|
||||
* [#13381](https://github.com/cosmos/cosmos-sdk/pull/13381) Add uint `IsNil` method.
|
||||
* [#12634](https://github.com/cosmos/cosmos-sdk/pull/12634) Move `sdk.Dec` to math package, call it `LegacyDec`.
|
||||
* [#14166](https://github.com/cosmos/cosmos-sdk/pull/14166) Add generics versions of Max and Min, catering to all numeric types and allow for variadic calls, replacing the prior typed and strenous code
|
||||
|
||||
@ -12,6 +12,7 @@ require (
|
||||
github.com/kr/pretty v0.3.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.8.1 // indirect
|
||||
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
||||
@ -23,6 +23,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o=
|
||||
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
|
||||
29
math/max_min.go
Normal file
29
math/max_min.go
Normal file
@ -0,0 +1,29 @@
|
||||
package math
|
||||
|
||||
import "golang.org/x/exp/constraints"
|
||||
|
||||
func Max[T constraints.Ordered](a, b T, rest ...T) T {
|
||||
max := a
|
||||
if b > a {
|
||||
max = b
|
||||
}
|
||||
for _, val := range rest {
|
||||
if val > max {
|
||||
max = val
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
func Min[T constraints.Ordered](a, b T, rest ...T) T {
|
||||
min := a
|
||||
if b < a {
|
||||
min = b
|
||||
}
|
||||
for _, val := range rest {
|
||||
if val < min {
|
||||
min = val
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
19
math/max_min_test.go
Normal file
19
math/max_min_test.go
Normal file
@ -0,0 +1,19 @@
|
||||
package math
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMax(t *testing.T) {
|
||||
maxInt := Max(10, -10, 20, 1_000_000, 10, 8, -11_000_000, 20)
|
||||
require.Equal(t, 1_000_000, maxInt, "invalid max for int")
|
||||
minInt := Min(10, -10, 20, 1_000_000, 10, 8, -11_000_000, 20)
|
||||
require.Equal(t, -11_000_000, minInt, "invalid min for int")
|
||||
|
||||
maxf64 := Max(10.1, -10.1, 20.8, 1_000_000.9, 10.5, 8.4, -11_000_000.9, 20.7)
|
||||
require.Equal(t, 1_000_000.9, maxf64, "invalid max for float64")
|
||||
minf64 := Min(10.1, -10.1, 20.8, 1_000_000.9, 10.5, 8.4, -11_000_000.9, 20.7)
|
||||
require.Equal(t, -11_000_000.9, minf64, "invalid min for float64")
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user