mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
fix: fix issue #292
This commit is contained in:
@@ -145,10 +145,14 @@ func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string
|
||||
|
||||
// Max return max value of numbers.
|
||||
// Play: https://go.dev/play/p/cN8DHI0rTkH
|
||||
func Max[T constraints.Integer | constraints.Float](numbers ...T) T {
|
||||
max := numbers[0]
|
||||
func Max[T constraints.Ordered](items ...T) T {
|
||||
if len(items) < 1 {
|
||||
panic("mathutil.Max: empty list")
|
||||
}
|
||||
|
||||
for _, v := range numbers {
|
||||
max := items[0]
|
||||
|
||||
for _, v := range items {
|
||||
if max < v {
|
||||
max = v
|
||||
}
|
||||
@@ -181,10 +185,14 @@ func MaxBy[T any](slice []T, comparator func(T, T) bool) T {
|
||||
|
||||
// Min return min value of numbers.
|
||||
// Play: https://go.dev/play/p/21BER_mlGUj
|
||||
func Min[T constraints.Integer | constraints.Float](numbers ...T) T {
|
||||
min := numbers[0]
|
||||
func Min[T constraints.Ordered](items ...T) T {
|
||||
if len(items) < 1 {
|
||||
panic("mathutil.min: empty list")
|
||||
}
|
||||
|
||||
for _, v := range numbers {
|
||||
min := items[0]
|
||||
|
||||
for _, v := range items {
|
||||
if min > v {
|
||||
min = v
|
||||
}
|
||||
|
||||
@@ -178,6 +178,7 @@ func TestMax(t *testing.T) {
|
||||
assert.Equal(0, Max(0, 0))
|
||||
assert.Equal(3, Max(1, 2, 3))
|
||||
assert.Equal(1.4, Max(1.2, 1.4, 1.1, 1.4))
|
||||
assert.Equal("abc", Max("a", "ab", "abc"))
|
||||
|
||||
type Integer int
|
||||
assert.Equal(Integer(1), Max(Integer(1), Integer(0)))
|
||||
@@ -212,6 +213,8 @@ func TestMin(t *testing.T) {
|
||||
assert.Equal(0, Min(0, 0))
|
||||
assert.Equal(1, Min(1, 2, 3))
|
||||
assert.Equal(1.1, Min(1.2, 1.4, 1.1, 1.4))
|
||||
|
||||
assert.Equal("a", Min("a", "ab", "abc"))
|
||||
}
|
||||
|
||||
func TestMinBy(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user