-
Notifications
You must be signed in to change notification settings - Fork 7
/
min.go
33 lines (30 loc) · 577 Bytes
/
min.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package statistics
import (
"math"
"github.com/theriault/maths"
)
// Min returns the smallest value.
//
// This function returns the value as the same underlying type.
// Nullary invocation returns the default value of the type.
//
// Time complexity: O(n)
// Space complexity: O(1)
//
// https://en.wikipedia.org/wiki/Sample_maximum_and_minimum
func Min[A maths.Integer | maths.Float](a ...A) A {
if len(a) == 0 {
var a A
return a
}
min := a[0]
for _, v := range a {
if math.IsNaN(float64(v)) {
return A(v)
}
if v < min {
min = v
}
}
return min
}