From d90060c6a24f9e4d6b0b3707e548d7d7b429c52e Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 16 Jan 2023 14:42:13 +0800 Subject: [PATCH] stream --- stream/simpleStream.go | 4 ++-- stream/simpleStream_test.go | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/stream/simpleStream.go b/stream/simpleStream.go index e35e6ec..101b66d 100644 --- a/stream/simpleStream.go +++ b/stream/simpleStream.go @@ -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 { diff --git a/stream/simpleStream_test.go b/stream/simpleStream_test.go index c85bc3f..6b4b692 100644 --- a/stream/simpleStream_test.go +++ b/stream/simpleStream_test.go @@ -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) } })