bbgo_origin/pkg/fixedpoint/reduce.go

26 lines
397 B
Go
Raw Permalink Normal View History

2022-09-07 04:28:13 +00:00
package fixedpoint
type Reducer func(prev, curr Value) Value
2022-09-07 06:00:26 +00:00
func SumReducer(prev, curr Value) Value {
return prev.Add(curr)
}
func Reduce(values []Value, reducer Reducer, a ...Value) Value {
init := Zero
if len(a) > 0 {
init = a[0]
}
2022-09-07 04:28:13 +00:00
if len(values) == 0 {
return init
}
r := reducer(init, values[0])
for i := 1; i < len(values); i++ {
r = reducer(r, values[i])
}
return r
}