Co-authored-by: Ruidy <rnemausat@newstore.com>
This commit is contained in:
Ruidy 2022-02-20 10:34:09 -04:00 committed by GitHub
parent 28f85bdc94
commit 482e553263
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

11
sum.go Normal file
View 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
View 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))
}