underscore/sum.go
Ruidy c53d46816f
refactor: migrate to Go 1.22 slices/cmp, update linters
Replace usage of golang.org/x/exp/constraints with Go 1.22 cmp/slices.
Update .golangci.yml to new v2 format and enable gofmt/goimports.
Refactor imports and type constraints across codebase for consistency.
2025-09-01 23:08:10 -04:00

23 lines
453 B
Go

package underscore
import (
"cmp"
)
// Sum adds elements of the slice.
func Sum[T cmp.Ordered](values []T) (sum T) {
for _, v := range values {
sum += v
}
return sum
}
// SumMap sums the values you select from your struct, basically a sort cut instead of
// having to perform a [Map] followed by a [Sum].
func SumMap[T any, R cmp.Ordered](list []T, selector func(T) R) (sum R) {
for _, v := range list {
sum += selector(v)
}
return sum
}