MCPcopy Create free account
hub / github.com/Effect-TS/effect / reduceWhileArray

Function reduceWhileArray

packages/effect/src/Sink.ts:1394–1415  ·  view source on GitHub ↗
(
  initial: LazyArg<S>,
  contFn: Predicate<S>,
  f: (s: S, input: NonEmptyReadonlyArray<In>) => S
)

Source from the content-addressed store, hash-verified

1392 * @since 4.0.0
1393 */
1394export const reduceWhileArray = <S, In>(
1395 initial: LazyArg<S>,
1396 contFn: Predicate<S>,
1397 f: (s: S, input: NonEmptyReadonlyArray<In>) => S
1398): Sink<S, In> =>
1399 fromTransform((upstream) => {
1400 let state = initial()
1401 if (!contFn(state)) {
1402 return Effect.succeed([state] as const)
1403 }
1404 return upstream.pipe(
1405 Effect.flatMap((arr) => {
1406 state = f(state, arr)
1407 if (!contFn(state)) {
1408 return Cause.done()
1409 }
1410 return Effect.void
1411 }),
1412 Effect.forever({ disableYield: true }),
1413 Pull.catchDone(() => Effect.succeed([state] as const))
1414 )
1415 })
1416
1417/**
1418 * A sink that effectfully reduces non-empty input arrays from the provided

Callers

nothing calls this directly

Calls 6

fromTransformFunction · 0.70
pipeMethod · 0.65
initialFunction · 0.50
fFunction · 0.50
succeedMethod · 0.45
doneMethod · 0.45

Tested by

no test coverage detected