-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
51 lines (41 loc) · 876 Bytes
/
number.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
/*
* Author: fasion
* Created time: 2023-09-21 11:14:51
* Last Modified by: fasion
* Last Modified time: 2023-09-21 11:33:21
*/
package stl
import "golang.org/x/exp/constraints"
func Max[Datas ~[]Data, Data constraints.Ordered](datas Datas, _default Data) (result Data) {
if len(datas) == 0 {
return _default
}
result = datas[0]
for _, data := range datas {
if data > result {
result = data
}
}
return
}
func Min[Datas ~[]Data, Data constraints.Ordered](datas Datas, _default Data) (result Data) {
if len(datas) == 0 {
return _default
}
result = datas[0]
for _, data := range datas {
if data < result {
result = data
}
}
return
}
type Addend interface {
constraints.Integer | constraints.Float
}
func Sum[Datas ~[]Data, Data Addend](datas Datas, start Data) Data {
for _, data := range datas {
start += data
}
return start
}