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

Function fold

packages/effect/src/Sink.ts:755–777  ·  view source on GitHub ↗
(
  s: LazyArg<S>,
  contFn: Predicate<S>,
  f: (s: S, input: In) => Effect.Effect<S, E, R>
)

Source from the content-addressed store, hash-verified

753 * @since 2.0.0
754 */
755export const fold = <S, In, E = never, R = never>(
756 s: LazyArg<S>,
757 contFn: Predicate<S>,
758 f: (s: S, input: In) => Effect.Effect<S, E, R>
759): Sink<S, In, In, E, R> =>
760 fromTransform((upstream) => {
761 let state = s()
762 return Effect.gen(function*() {
763 while (true) {
764 const arr = yield* upstream
765 for (let i = 0; i < arr.length; i++) {
766 state = yield* f(state, arr[i])
767 if (contFn(state)) continue
768 return [
769 state,
770 (i + 1) < arr.length ? (arr.slice(i + 1) as any) : undefined
771 ] as const
772 }
773 }
774 }).pipe(
775 Pull.catchDone(() => Effect.succeed<End<S, In>>([state]))
776 )
777 })
778
779/**
780 * Folds non-empty input arrays into state with an effectful function.

Callers 3

foldUntilFunction · 0.85
everyFunction · 0.85
someFunction · 0.85

Calls 4

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

Tested by

no test coverage detected