| 753 | * @since 2.0.0 |
| 754 | */ |
| 755 | export const fold = <S, In, E = never, R = never>( |
| 756 | s: LazyArg<S>, |
| 757 | contFn: Predicate<S>, |
| 758 | f: (s: S, input: In) => Effect.Effect<S, E, R> |
| 759 | ): Sink<S, In, In, E, R> => |
| 760 | fromTransform((upstream) => { |
| 761 | let state = s() |
| 762 | return Effect.gen(function*() { |
| 763 | while (true) { |
| 764 | const arr = yield* upstream |
| 765 | for (let i = 0; i < arr.length; i++) { |
| 766 | state = yield* f(state, arr[i]) |
| 767 | if (contFn(state)) continue |
| 768 | return [ |
| 769 | state, |
| 770 | (i + 1) < arr.length ? (arr.slice(i + 1) as any) : undefined |
| 771 | ] as const |
| 772 | } |
| 773 | } |
| 774 | }).pipe( |
| 775 | Pull.catchDone(() => Effect.succeed<End<S, In>>([state])) |
| 776 | ) |
| 777 | }) |
| 778 | |
| 779 | /** |
| 780 | * Folds non-empty input arrays into state with an effectful function. |