diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 502e2e1b36..f153b00a09 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -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 diff --git a/math/go.mod b/math/go.mod index 1398425b09..0db7641c79 100644 --- a/math/go.mod +++ b/math/go.mod @@ -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 diff --git a/math/go.sum b/math/go.sum index e82db57c1b..67b55288d3 100644 --- a/math/go.sum +++ b/math/go.sum @@ -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= diff --git a/math/max_min.go b/math/max_min.go new file mode 100644 index 0000000000..407dd816ce --- /dev/null +++ b/math/max_min.go @@ -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 +} diff --git a/math/max_min_test.go b/math/max_min_test.go new file mode 100644 index 0000000000..ae700f7562 --- /dev/null +++ b/math/max_min_test.go @@ -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") +}