mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
Created a Tuple struct as some of the new functions require you to return a new slice with two fields which is the result of the new functions Created the Join, JoinProjection, Range, SumMap, Zip functions, ecah fuction is documented with how it works and had a unit test or maybe more
21 lines
481 B
Go
21 lines
481 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
|
|
}
|
|
|
|
// Sums the values you select from your struct, basically a sort cut instead of
|
|
// having to perform a u.Map followed by a u.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
|
|
}
|