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

Function unfold

packages/effect/src/Stream.ts:1451–1462  ·  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

1449 * @since 2.0.0
1450 */
1451export const unfold = <S, A, E, R>(
1452 s: S,
1453 f: (s: S) => Effect.Effect<readonly [A, S] | undefined, E, R>
1454): Stream<A, E, R> =>
1455 fromPull(Effect.sync(() => {
1456 let state = s
1457 return Effect.flatMap(Effect.suspend(() => f(state)), (next) => {
1458 if (next === undefined) return Cause.done()
1459 state = next[1]
1460 return Effect.succeed(Arr.of(next[0]))
1461 })
1462 }))
1463
1464/**
1465 * 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