underscore/sum.go
Ruidy fbf58eff42
Refresh documentation (#39)
* minor documentation fixes

* fix Hugo warnings

* add openssf badge
2024-12-03 09:29:53 +01:00

21 lines
489 B
Go

package underscore
import "golang.org/x/exp/constraints"
// Sum adds elements of the slice.
func Sum[T constraints.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 constraints.Ordered](list []T, selector func(T) R) (sum R) {
for _, v := range list {
sum += selector(v)
}
return sum
}