MCPcopy Create free account
hub / github.com/effect-app/libs / reduceWhileArray

Function reduceWhileArray

repos/effect/packages/effect/src/Sink.ts:1295–1318  ·  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

1293 })
1294
1295/**
1296 * A sink that effectfully reduces input elements from the provided `initial`
1297 * state with `f` while the specified `predicate` returns `true`.
1298 *
1299 * @category folding
1300 * @since 4.0.0
1301 */
1302export const reduceWhileEffect = <S, In, E, R>(
1303 initial: LazyArg<S>,
1304 predicate: Predicate<S>,
1305 f: (s: S, input: In) => Effect.Effect<S, E, R>
1306): Sink<S, In, In, E, R> =>
1307 fromTransform((upstream) => {
1308 let state = initial()
1309 let leftover: NonEmptyReadonlyArray<In> | undefined = undefined
1310 if (!predicate(state)) {
1311 return Effect.succeed([state] as const)
1312 }
1313 return upstream.pipe(
1314 Effect.flatMap((arr) => {
1315 let i = 0
1316 return Effect.whileLoop({
1317 while: () => i < arr.length,
1318 body: constant(Effect.flatMap(Effect.suspend(() => f(state, arr[i++])), (s) => {
1319 state = s
1320 if (!predicate(state)) {
1321 if (i < arr.length) {

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…