-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreduce.go
43 lines (34 loc) · 1.01 KB
/
reduce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ranges
// Reduce produces a singe value from a range by calling a callback to combine the values.
// The seed value will be used as the first value for `a` in `cb(a, b)`.
func Reduce[T, U any](r InputRange[T], cb func(a U, b T) U, seed U) U {
for !r.Empty() {
seed = cb(seed, r.Front())
r.PopFront()
}
return seed
}
// ReduceS is `Reduce` accepting a slice.
func ReduceS[T, U any](r []T, cb func(a U, b T) U, seed U) U {
return Reduce(SliceRange(r), cb, seed)
}
// ReduceNoSeed is `Reduce` where the the range is assumed not to be empty, and
// the seed is the front of the range.
//
// Panics when the range is empty.
func ReduceNoSeed[T any](r InputRange[T], cb func(a T, b T) T) T {
if r.Empty() {
panic("Cannot reduce an empty range")
}
seed := r.Front()
r.PopFront()
for !r.Empty() {
seed = cb(seed, r.Front())
r.PopFront()
}
return seed
}
// ReduceNoSeedS is `ReduceNoSeed` accepting a slice.
func ReduceNoSeedS[T any](r []T, cb func(a T, b T) T) T {
return ReduceNoSeed(SliceRange(r), cb)
}