Skip to content

Commit

Permalink
Update MinFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
dimriou committed Dec 7, 2023
1 parent 730fc11 commit 64e1b94
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions common/internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package utils

import (
"cmp"
"slices"
"time"

"github.com/jpillora/backoff"
Expand All @@ -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)
}

0 comments on commit 64e1b94

Please sign in to comment.