| 783 | * @since 2.0.0 |
| 784 | */ |
| 785 | export const fold = <S, In, E = never, R = never>( |
| 786 | s: LazyArg<S>, |
| 787 | contFn: Predicate<S>, |
| 788 | f: (s: S, input: In) => Effect.Effect<S, E, R> |
| 789 | ): Sink<S, In, In, E, R> => |
| 790 | fromTransform((upstream) => { |
| 791 | let state = s() |
| 792 | return Effect.gen(function*() { |
| 793 | while (true) { |
| 794 | const arr = yield* upstream |
| 795 | for (let i = 0; i < arr.length; i++) { |
| 796 | state = yield* f(state, arr[i]) |
| 797 | if (contFn(state)) continue |
| 798 | return [ |
| 799 | state, |
| 800 | (i + 1) < arr.length ? (arr.slice(i + 1) as any) : undefined |
| 801 | ] as const |
| 802 | } |
| 803 | } |
| 804 | }).pipe( |
| 805 | Pull.catchDone(() => Effect.succeed<End<S, In>>([state])) |
| 806 | ) |
| 807 | }) |
| 808 | |
| 809 | /** |
| 810 | * Folds non-empty input arrays into state with an effectful function. |