Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-wolves-reduce.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix `Sink.reduceWhileArray` applying its reducer more than once per input array.
8 changes: 3 additions & 5 deletions packages/effect/src/Sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1336,11 +1336,9 @@ export const reduceWhileArray = <S, In>(
}
return upstream.pipe(
Effect.flatMap((arr) => {
for (let i = 0; i < arr.length; i++) {
state = f(state, arr)
if (!contFn(state)) {
return Cause.done()
}
state = f(state, arr)
if (!contFn(state)) {
return Cause.done()
}
return Effect.void
}),
Expand Down
12 changes: 12 additions & 0 deletions packages/effect/test/Sink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ describe("Sink", () => {
strictEqual(result, 45)
}))
})

describe("reduceWhileArray", () => {
it.effect("applies the reducer once per non-empty input array", () =>
Effect.gen(function*() {
const result = yield* Stream.fromArrays([1, 2, 3]).pipe(
Stream.run(Sink.reduceWhileArray(() => 0, constTrue, (count) => count + 1))
)

strictEqual(result, 1, "the reducer must run once for each input array")
}))
})

describe("reduceWhileEffect", () => {
it.effect("short circuits", () =>
Effect.gen(function*() {
Expand Down
Loading