mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
1004 B
1004 B
name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | signatures | playUrl | variantHelpers | similarHelpers | position | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Sum / SumBy | sum | it/math.go#L59 | it | math |
|
https://go.dev/play/p/nHbGFOEIeTa |
|
|
10 |
Sums values from a sequence. SumBy applies a transform and sums the results. Returns 0 for empty sequences.
Examples:
sum := it.Sum(it.RangeFrom(1, 5))
// 1 + 2 + 3 + 4 + 5 == 15
type User struct { Name string; Score int }
users := func(yield func(User) bool) {
_ = yield(User{"a", 3})
_ = yield(User{"b", 7})
}
total := it.SumBy(iter.Seq[User](users), func(u User) int { return u.Score })
// total == 10