-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathminmax.go
More file actions
34 lines (30 loc) · 761 Bytes
/
minmax.go
File metadata and controls
34 lines (30 loc) · 761 Bytes
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
package missinggo
import "reflect"
func Max(_less interface{}, vals ...interface{}) interface{} {
ret := reflect.ValueOf(vals[0])
retType := ret.Type()
less := reflect.ValueOf(_less)
for _, _v := range vals[1:] {
v := reflect.ValueOf(_v).Convert(retType)
out := less.Call([]reflect.Value{ret, v})
if out[0].Bool() {
ret = v
}
}
return ret.Interface()
}
func MaxInt(first int64, rest ...interface{}) int64 {
return Max(func(l, r interface{}) bool {
return l.(int64) < r.(int64)
}, append([]interface{}{first}, rest...)...).(int64)
}
func MinInt(first interface{}, rest ...interface{}) int64 {
ret := reflect.ValueOf(first).Int()
for _, _i := range rest {
i := reflect.ValueOf(_i).Int()
if i < ret {
ret = i
}
}
return ret
}