underscore/sum.go
Andy Long 0bc3a54efd
Adding some new funky functions (#32)
* Adding some new funky functions which I find useful

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

* Added in an OrderBy function

* Documentation comment for OrderBy which I missed out

* Adding a Unit test for JoinProject function

Updated the comments on the Join & OrderBy functions so they make a little more sense.

Covered an extra test case with the Join test, where the left set has more data than the right and so the Right handside array of the join is empty
2022-09-04 09:21:31 +02:00

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
}