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" "github/fthvgb1/wp-go/taskPools"
) )
func Reduce[T any, S any](s SimpleSliceStream[S], fn func(S, T) T) (r T) { func Reduce[T any, S any](s SimpleSliceStream[S], fn func(S, T) T, init T) (r T) {
return helper.SliceReduce(s.arr, fn, r) return helper.SliceReduce(s.arr, fn, init)
} }
type SimpleSliceStream[T any] struct { type SimpleSliceStream[T any] struct {

View File

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