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

Function reduceArray

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

Source from the content-addressed store, hash-verified

1401 * @since 4.0.0
1402 */
1403export const reduceArray = <S, In>(
1404 initial: LazyArg<S>,
1405 f: (s: S, input: NonEmptyReadonlyArray<In>) => S
1406): Sink<S, In> =>
1407 fromTransform((upstream) => {
1408 let state = initial()
1409 return upstream.pipe(
1410 Effect.flatMap((arr) => {
1411 state = f(state, arr)
1412 return Effect.void
1413 }),
1414 Effect.forever({ disableYield: true }),
1415 Pull.catchDone(() => Effect.succeed([state] as const))
1416 )
1417 })
1418
1419/**
1420 * A sink that reduces its inputs using the provided effectful function `f`

Callers 3

reduceFunction · 0.85
Sink.tsFile · 0.85
collectFunction · 0.85

Calls 5

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

Tested by

no test coverage detected