Hyperlinkv0.8.0-beta.28

Sink

Sink.foldUntilconsteffect/Sink.ts:798
<S, In, E = never, R = never>(
  s: LazyArg<S>,
  max: number,
  f: (s: S, input: In) => Effect.Effect<S, E, R>
): Sink<S, In, In, E, R>

Folds input elements into state until the specified maximum number of elements has been consumed or the upstream stream ends.

Details

If the sink stops in the middle of a pulled array, the remaining elements from that array are returned as leftovers.

folding
Source effect/Sink.ts:79812 lines
export const foldUntil = <S, In, E = never, R = never>(
  s: LazyArg<S>,
  max: number,
  f: (s: S, input: In) => Effect.Effect<S, E, R>
): Sink<S, In, In, E, R> =>
  fold<readonly [S, number], In, E, R>(
    () => [s(), 0],
    (tuple) => tuple[1] < max,
    ([output, count], input) => Effect.map(f(output, input), (s) => [s, count + 1] as const)
  ).pipe(
    map((tuple) => tuple[0])
  )