-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimax.go
65 lines (56 loc) · 990 Bytes
/
minimax.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package fu
import "golang.org/x/exp/constraints"
func Min[T constraints.Ordered](a ...T) T {
_, v := MinIndex(a...)
return v
}
func MinOr[T constraints.Ordered](dflt T, a ...T) T {
if len(a) > 0 {
return Min(a...)
}
return dflt
}
func MinOf[T constraints.Ordered](v T, a ...T) T {
if len(a) > 0 {
if x := Min(a...); x < v {
return x
}
}
return v
}
func MinIndex[T constraints.Ordered](a ...T) (int, T) {
j, y := 0, a[0]
for i, x := range a[1:] {
if x < y {
j, y = i, x
}
}
return j, y
}
func Max[T constraints.Ordered](a ...T) T {
_, v := MaxIndex(a...)
return v
}
func MaxOr[T constraints.Ordered](dflt T, a ...T) T {
if len(a) > 0 {
return Max(a...)
}
return dflt
}
func MaxOf[T constraints.Ordered](v T, a ...T) T {
if len(a) > 0 {
if x := Max(a...); x > v {
return x
}
}
return v
}
func MaxIndex[T constraints.Ordered](a ...T) (int, T) {
j, y := 0, a[0]
for i, x := range a[1:] {
if x > y {
j, y = i, x
}
}
return j, y
}