-
Notifications
You must be signed in to change notification settings - Fork 3
/
math.go
46 lines (41 loc) · 1.1 KB
/
math.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
package mathKit
import (
"github.com/samber/lo"
"golang.org/x/exp/constraints"
)
// Clamp clamps number within the inclusive lower and upper bounds.
/*
case value < min: 返回min
case value > max: 返回max
case others: 返回value
e.g.
(0, -10, 10) => 0
(-42, -10, 10) => -10
(42, -10, 10) => 10
*/
func Clamp[T constraints.Ordered](value T, min T, max T) T {
return lo.Clamp(value, min, max)
}
// Sum 求和
/*
e.g.
([]int{0, 1, 2, 3}) => 6
*/
func Sum[T constraints.Float | constraints.Integer | constraints.Complex](s []T) T {
return lo.Sum(s)
}
// SumBy 求和
/*
@param s 可以为nil(此时返回0)
@param iteratee (1) 不能为nil(除非s == nil),否则会导致panic: runtime error: invalid memory address or nil pointer dereference
(2) 传参为T类型,返回值为R类型
e.g.
i := mathKit.SumBy[string, int]([]string{"0", "1", "2"}, func(item string) int {
tmp, _ := strconv.Atoi(item)
return tmp
})
fmt.Println(i) // 3
*/
func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](s []T, iteratee func(item T) R) R {
return lo.SumBy(s, iteratee)
}