| 1293 | }) |
| 1294 | |
| 1295 | /** |
| 1296 | * A sink that effectfully reduces input elements from the provided `initial` |
| 1297 | * state with `f` while the specified `predicate` returns `true`. |
| 1298 | * |
| 1299 | * @category folding |
| 1300 | * @since 4.0.0 |
| 1301 | */ |
| 1302 | export const reduceWhileEffect = <S, In, E, R>( |
| 1303 | initial: LazyArg<S>, |
| 1304 | predicate: Predicate<S>, |
| 1305 | f: (s: S, input: In) => Effect.Effect<S, E, R> |
| 1306 | ): Sink<S, In, In, E, R> => |
| 1307 | fromTransform((upstream) => { |
| 1308 | let state = initial() |
| 1309 | let leftover: NonEmptyReadonlyArray<In> | undefined = undefined |
| 1310 | if (!predicate(state)) { |
| 1311 | return Effect.succeed([state] as const) |
| 1312 | } |
| 1313 | return upstream.pipe( |
| 1314 | Effect.flatMap((arr) => { |
| 1315 | let i = 0 |
| 1316 | return Effect.whileLoop({ |
| 1317 | while: () => i < arr.length, |
| 1318 | body: constant(Effect.flatMap(Effect.suspend(() => f(state, arr[i++])), (s) => { |
| 1319 | state = s |
| 1320 | if (!predicate(state)) { |
| 1321 | if (i < arr.length) { |