Hyperlinkv0.8.0-beta.28

Sink

Sink.foldArrayconsteffect/Sink.ts:767
<S, In, E = never, R = never>(
  s: LazyArg<S>,
  contFn: Predicate<S>,
  f: (
    s: S,
    input: Arr.NonEmptyReadonlyArray<In>
  ) => Effect.Effect<S, E, R>
): Sink<S, In, never, E, R>

Folds non-empty input arrays into state with an effectful function.

When to use

Use to update state with an effectful function once per pulled non-empty input array when batch-level processing is the natural unit.

Details

The initial state is evaluated lazily. After each pulled array is folded, the sink continues while contFn returns true; otherwise it completes with the current state.

Source effect/Sink.ts:76718 lines
export const foldArray = <S, In, E = never, R = never>(
  s: LazyArg<S>,
  contFn: Predicate<S>,
  f: (s: S, input: Arr.NonEmptyReadonlyArray<In>) => Effect.Effect<S, E, R>
): Sink<S, In, never, E, R> =>
  fromTransform((upstream) => {
    let state = s()
    return Effect.gen(function*() {
      while (true) {
        const arr = yield* upstream
        state = yield* f(state, arr)
        if (contFn(state)) continue
        return [state] as const
      }
    }).pipe(
      Pull.catchDone(() => Effect.succeed<End<S>>([state]))
    )
  })