mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 10:36:43 +00:00
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.
23 lines
453 B
Go
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
|
|
}
|