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

Function fold

packages/effect/src/Sink.ts:785–807  ·  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

783 * @since 2.0.0
784 */
785export const fold = <S, In, E = never, R = never>(
786 s: LazyArg<S>,
787 contFn: Predicate<S>,
788 f: (s: S, input: In) => Effect.Effect<S, E, R>
789): Sink<S, In, In, E, R> =>
790 fromTransform((upstream) => {
791 let state = s()
792 return Effect.gen(function*() {
793 while (true) {
794 const arr = yield* upstream
795 for (let i = 0; i < arr.length; i++) {
796 state = yield* f(state, arr[i])
797 if (contFn(state)) continue
798 return [
799 state,
800 (i + 1) < arr.length ? (arr.slice(i + 1) as any) : undefined
801 ] as const
802 }
803 }
804 }).pipe(
805 Pull.catchDone(() => Effect.succeed<End<S, In>>([state]))
806 )
807 })
808
809/**
810 * Folds non-empty input arrays into state with an effectful function.

Callers 4

foldUntilFunction · 0.70
everyFunction · 0.70
someFunction · 0.70
tokenizeFunction · 0.50

Calls 6

sFunction · 0.85
sliceMethod · 0.80
fromTransformFunction · 0.70
pipeMethod · 0.65
fFunction · 0.50
succeedMethod · 0.45

Tested by

no test coverage detected