-
Notifications
You must be signed in to change notification settings - Fork 3
/
sum.go
35 lines (30 loc) · 936 Bytes
/
sum.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
package mathKit
import (
"github.com/samber/lo"
"golang.org/x/exp/constraints"
)
// Sum 求和.
/*
PS: 不使用lancet的mathutil,原因: 泛型更广.
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 求和.
/*
PS: 不使用lancet的mathutil,原因: 泛型更广、没有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)
}