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

Function unfold

packages/effect/src/Stream.ts:1576–1587  ·  view source on GitHub ↗
(
  s: S,
  f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>
)

Source from the content-addressed store, hash-verified

1574 * @since 2.0.0
1575 */
1576export const unfold = <S, A, E, R>(
1577 s: S,
1578 f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>
1579): Stream<A, E, R> =>
1580 fromPull(Effect.sync(() => {
1581 let state = s
1582 return Effect.flatMap(Effect.suspend(() => f(state)), (next) => {
1583 if (next === undefined) return Cause.done()
1584 state = next[1]
1585 return Effect.succeed(Arr.of(next[0]))
1586 })
1587 }))
1588
1589/**
1590 * Creates a stream by repeatedly evaluating an effectful page function.

Callers 1

iterateFunction · 0.70

Calls 6

fromPullFunction · 0.70
ofMethod · 0.65
fFunction · 0.50
syncMethod · 0.45
doneMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected