mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
parent
28f85bdc94
commit
482e553263
2 changed files with 26 additions and 0 deletions
11
sum.go
Normal file
11
sum.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package underscore
|
||||
|
||||
import "constraints"
|
||||
|
||||
// Sum adds elements of the slice.
|
||||
func Sum[T constraints.Ordered](values []T) (sum T) {
|
||||
for _, v := range values {
|
||||
sum += v
|
||||
}
|
||||
return sum
|
||||
}
|
||||
15
sum_test.go
Normal file
15
sum_test.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package underscore_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
u "github.com/rjNemo/underscore"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
want := 45
|
||||
|
||||
assert.Equal(t, want, u.Sum(nums))
|
||||
}
|
||||
Loading…
Reference in a new issue