diff --git a/reduce.go b/reduce.go index bbcd9e9..9280e29 100644 --- a/reduce.go +++ b/reduce.go @@ -56,7 +56,10 @@ func Reduce[A any](in <-chan Try[A], n int, f func(A, A) (A, error)) (result A, return Try[A]{Value: res} // the only non-dummy return }) - setReturns(res.Value, ok, nil) + if res.Error != nil { + ok = false + } + setReturns(res.Value, ok, res.Error) }() once.Wait() diff --git a/reduce_test.go b/reduce_test.go index 2433e8e..04b2e85 100644 --- a/reduce_test.go +++ b/reduce_test.go @@ -24,6 +24,33 @@ func TestReduce(t *testing.T) { th.ExpectDrainedChan(t, in) }) + t.Run(th.Name("single item", n), func(t *testing.T) { + t.Run("no error", func(t *testing.T) { + in := FromSlice([]int{5}, nil) + + out, ok, err := Reduce(in, n, func(x, y int) (int, error) { + return x + y, nil + }) + + th.ExpectNoError(t, err) + th.ExpectValue(t, out, 5) + th.ExpectValue(t, ok, true) + th.ExpectDrainedChan(t, in) + }) + + t.Run("error", func(t *testing.T) { + in := FromSlice([]int{}, fmt.Errorf("err0")) + + _, ok, err := Reduce(in, n, func(x, y int) (int, error) { + return x + y, nil + }) + + th.ExpectError(t, err, "err0") + th.ExpectValue(t, ok, false) + th.ExpectDrainedChan(t, in) + }) + }) + t.Run(th.Name("no errors", n), func(t *testing.T) { in := FromChan(th.FromRange(0, 100), nil)