This commit is contained in:
xing 2023-01-16 14:42:13 +08:00
parent 51f0179ca5
commit d90060c6a2
2 changed files with 7 additions and 5 deletions

View File

@ -6,8 +6,8 @@ import (
"github/fthvgb1/wp-go/taskPools"
)
func Reduce[T any, S any](s SimpleSliceStream[S], fn func(S, T) T) (r T) {
return helper.SliceReduce(s.arr, fn, r)
func Reduce[T any, S any](s SimpleSliceStream[S], fn func(S, T) T, init T) (r T) {
return helper.SliceReduce(s.arr, fn, init)
}
type SimpleSliceStream[T any] struct {

View File

@ -286,8 +286,9 @@ func TestSimpleSliceStream_ParallelMap(t *testing.T) {
func TestReduce(t *testing.T) {
type args[S, T int] struct {
s SimpleSliceStream[S]
fn func(S, T) T
s SimpleSliceStream[S]
fn func(S, T) T
init T
}
type testCase[S, T int] struct {
name string
@ -301,13 +302,14 @@ func TestReduce(t *testing.T) {
s, func(i, r int) int {
return i + r
},
0,
},
wantR: helper.Sum(s.Result()...),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotR := Reduce(tt.args.s, tt.args.fn); !reflect.DeepEqual(gotR, tt.wantR) {
if gotR := Reduce(tt.args.s, tt.args.fn, tt.args.init); !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("Reduce() = %v, want %v", gotR, tt.wantR)
}
})