mirror of
https://github.com/samber/lo.git
synced 2025-09-26 20:11:13 +08:00
doc: improve doc for lo.Sum
This commit is contained in:
@@ -10,6 +10,7 @@ Adding:
|
||||
- lo.CountValues
|
||||
- lo.CountValuesBy
|
||||
- lo.MapEntries
|
||||
- lo.Sum
|
||||
- TupleX.Unpack()
|
||||
|
||||
## 1.31.0 (2022-10-06)
|
||||
|
16
README.md
16
README.md
@@ -120,6 +120,7 @@ Supported math helpers:
|
||||
|
||||
- [Range / RangeFrom / RangeWithSteps](#range--rangefrom--rangewithsteps)
|
||||
- [Clamp](#clamp)
|
||||
- [Sum](#sum)
|
||||
- [SumBy](#sumby)
|
||||
|
||||
Supported helpers for strings:
|
||||
@@ -1142,9 +1143,24 @@ r3 := lo.Clamp(42, -10, 10)
|
||||
|
||||
[[play](https://go.dev/play/p/RU4lJNC2hlI)]
|
||||
|
||||
### Sum
|
||||
|
||||
Sums the values in a collection.
|
||||
|
||||
If collection is empty 0 is returned.
|
||||
|
||||
```go
|
||||
list := []int{1, 2, 3, 4, 5}
|
||||
sum := lo.Sum(list)
|
||||
// 15
|
||||
```
|
||||
|
||||
[[play](https://go.dev/play/p/upfeJVqs4Bt)]
|
||||
|
||||
### SumBy
|
||||
|
||||
Summarizes the values in a collection using the given return value from the iteration function.
|
||||
|
||||
If collection is empty 0 is returned.
|
||||
|
||||
```go
|
||||
|
25
math.go
25
math.go
@@ -63,21 +63,22 @@ func Clamp[T constraints.Ordered](value T, min T, max T) T {
|
||||
return value
|
||||
}
|
||||
|
||||
// SumBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 0 is returned.
|
||||
// Play: https://go.dev/play/p/Dz_a_7jN_ca
|
||||
func SumBy[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(T) R) R {
|
||||
var sum R = 0
|
||||
for _, item := range collection {
|
||||
sum = sum + iteratee(item)
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
// Sums the values in a slice.
|
||||
func Sum[T constraints.Float | constraints.Integer](collection []T) T {
|
||||
// Sum sums the values in a collection. If collection is empty 0 is returned.
|
||||
// Play: https://go.dev/play/p/upfeJVqs4Bt
|
||||
func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T {
|
||||
var sum T = 0
|
||||
for _, val := range collection {
|
||||
sum += val
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
// SumBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 0 is returned.
|
||||
// Play: https://go.dev/play/p/Dz_a_7jN_ca
|
||||
func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(T) R) R {
|
||||
var sum R = 0
|
||||
for _, item := range collection {
|
||||
sum = sum + iteratee(item)
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
@@ -47,6 +47,15 @@ func ExampleClamp() {
|
||||
// 10
|
||||
}
|
||||
|
||||
func ExampleSum() {
|
||||
list := []int{1, 2, 3, 4, 5}
|
||||
|
||||
sum := Sum(list)
|
||||
|
||||
fmt.Printf("%v", sum)
|
||||
// Output: 15
|
||||
}
|
||||
|
||||
func ExampleSumBy() {
|
||||
list := []string{"foo", "bar"}
|
||||
|
||||
|
32
math_test.go
32
math_test.go
@@ -65,6 +65,22 @@ func TestClamp(t *testing.T) {
|
||||
is.Equal(result3, 10)
|
||||
}
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := Sum([]float32{2.3, 3.3, 4, 5.3})
|
||||
result2 := Sum([]int32{2, 3, 4, 5})
|
||||
result3 := Sum([]uint32{2, 3, 4, 5})
|
||||
result4 := Sum([]uint32{})
|
||||
result5 := Sum([]complex128{4_4, 2_2})
|
||||
|
||||
is.Equal(result1, float32(14.900001))
|
||||
is.Equal(result2, int32(14))
|
||||
is.Equal(result3, uint32(14))
|
||||
is.Equal(result4, uint32(0))
|
||||
is.Equal(result5, complex128(6_6))
|
||||
}
|
||||
|
||||
func TestSumBy(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
@@ -73,23 +89,11 @@ func TestSumBy(t *testing.T) {
|
||||
result2 := SumBy([]int32{2, 3, 4, 5}, func(n int32) int32 { return n })
|
||||
result3 := SumBy([]uint32{2, 3, 4, 5}, func(n uint32) uint32 { return n })
|
||||
result4 := SumBy([]uint32{}, func(n uint32) uint32 { return n })
|
||||
result5 := SumBy([]complex128{4_4, 2_2}, func(n complex128) complex128 { return n })
|
||||
|
||||
is.Equal(result1, float32(14.900001))
|
||||
is.Equal(result2, int32(14))
|
||||
is.Equal(result3, uint32(14))
|
||||
is.Equal(result4, uint32(0))
|
||||
}
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := Sum([]float32{2.3, 3.3, 4, 5.3})
|
||||
result2 := Sum([]int32{2, 3, 4, 5})
|
||||
result3 := Sum([]uint32{2, 3, 4, 5})
|
||||
result4 := Sum([]uint32{})
|
||||
|
||||
is.Equal(result1, float32(14.900001))
|
||||
is.Equal(result2, int32(14))
|
||||
is.Equal(result3, uint32(14))
|
||||
is.Equal(result4, uint32(0))
|
||||
is.Equal(result5, complex128(6_6))
|
||||
}
|
||||
|
Reference in New Issue
Block a user