Skip to content

Commit ef07642

Browse files
fubhytim-smart
andauthored
Add reproduction for Sink issue (#6876)
Co-authored-by: Tim Smart <[email protected]>
1 parent f1e3a37 commit ef07642

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎.changeset/calm-wolves-reduce.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix `Sink.reduceWhileArray` applying its reducer more than once per input array.

‎packages/effect/src/Sink.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,11 +1336,9 @@ export const reduceWhileArray = <S, In>(
13361336
}
13371337
return upstream.pipe(
13381338
Effect.flatMap((arr) => {
1339-
for (let i = 0; i < arr.length; i++) {
1340-
state = f(state, arr)
1341-
if (!contFn(state)) {
1342-
return Cause.done()
1343-
}
1339+
state = f(state, arr)
1340+
if (!contFn(state)) {
1341+
return Cause.done()
13441342
}
13451343
return Effect.void
13461344
}),

‎packages/effect/test/Sink.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ describe("Sink", () => {
106106
strictEqual(result, 45)
107107
}))
108108
})
109+
110+
describe("reduceWhileArray", () => {
111+
it.effect("applies the reducer once per non-empty input array", () =>
112+
Effect.gen(function*() {
113+
const result = yield* Stream.fromArrays([1, 2, 3]).pipe(
114+
Stream.run(Sink.reduceWhileArray(() => 0, constTrue, (count) => count + 1))
115+
)
116+
117+
strictEqual(result, 1, "the reducer must run once for each input array")
118+
}))
119+
})
120+
109121
describe("reduceWhileEffect", () => {
110122
it.effect("short circuits", () =>
111123
Effect.gen(function*() {

0 commit comments

Comments
 (0)