From 64e1b9402f1ccafe2be9380ad96b9208026f2837 Mon Sep 17 00:00:00 2001 From: Dimitris Date: Thu, 7 Dec 2023 19:37:05 +0200 Subject: [PATCH] Update MinFunc --- common/internal/utils/utils.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/common/internal/utils/utils.go b/common/internal/utils/utils.go index 4442c2272df..1e285868c53 100644 --- a/common/internal/utils/utils.go +++ b/common/internal/utils/utils.go @@ -1,6 +1,8 @@ package utils import ( + "cmp" + "slices" "time" "github.com/jpillora/backoff" @@ -21,19 +23,14 @@ func NewRedialBackoff() backoff.Backoff { // MinFunc returns the minimum value of the given element array with respect // to the given key function. In the event U is not a compound type (e.g a // struct) an identity function can be provided. -func MinFunc[U any, T constraints.Ordered](elems []U, key func(U) T) T { +func MinFunc[U any, T constraints.Ordered](elems []U, f func(U) T) T { var min T if len(elems) == 0 { return min } - min = key(elems[0]) - for i := 1; i < len(elems); i++ { - v := key(elems[i]) - if v < min { - min = v - } - } - - return min + e := slices.MinFunc(elems, func(a, b U) int { + return cmp.Compare(f(a), f(b)) + }) + return f(e) }