mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
fixedpoint: add reducer and its tests
This commit is contained in:
parent
e161f4ec1a
commit
668180f8aa
|
@ -2,7 +2,16 @@ package fixedpoint
|
|||
|
||||
type Reducer func(prev, curr Value) Value
|
||||
|
||||
func Reduce(values []Value, init Value, reducer Reducer) Value {
|
||||
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]
|
||||
}
|
||||
|
||||
if len(values) == 0 {
|
||||
return init
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ func TestReduce(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.want, Reduce(tt.args.values, tt.args.init, tt.args.reducer), "Reduce(%v, %v, %v)", tt.args.values, tt.args.init, tt.args.reducer)
|
||||
assert.Equalf(t, tt.want, Reduce(tt.args.values, tt.args.reducer, tt.args.init), "Reduce(%v, %v, %v)", tt.args.values, tt.args.init, tt.args.reducer)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@ package fixedpoint
|
|||
|
||||
type Slice []Value
|
||||
|
||||
func (s Slice) Reduce(init Value, reducer Reducer) Value {
|
||||
return Reduce(s, init, reducer)
|
||||
func (s Slice) Reduce(reducer Reducer, a ...Value) Value {
|
||||
return Reduce(s, reducer, a...)
|
||||
}
|
||||
|
||||
// Defaults to ascending sort
|
||||
|
|
Loading…
Reference in New Issue
Block a user