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

Function paginate

packages/effect/src/Stream.ts:1496–1517  ·  view source on GitHub ↗
(
  s: S,
  f: (
    s: S
  ) => Effect.Effect<readonly [ReadonlyArray<A>, Option.Option<S>], E, R>
)

Source from the content-addressed store, hash-verified

1494 * @since 2.0.0
1495 */
1496export const paginate = <S, A, E = never, R = never>(
1497 s: S,
1498 f: (
1499 s: S
1500 ) => Effect.Effect<readonly [ReadonlyArray<A>, Option.Option<S>], E, R>
1501): Stream<A, E, R> =>
1502 fromPull(Effect.sync(() => {
1503 let state = s
1504 let done = false
1505 return Effect.suspend(function loop(): Pull.Pull<Arr.NonEmptyReadonlyArray<A>, E, void, R> {
1506 if (done) return Cause.done()
1507 return Effect.flatMap(f(state), ([a, s]) => {
1508 if (Option.isNone(s)) {
1509 done = true
1510 } else {
1511 state = s.value
1512 }
1513 if (!Arr.isReadonlyArrayNonEmpty(a)) return loop()
1514 return Effect.succeed(a)
1515 })
1516 })
1517 }))
1518
1519/**
1520 * Creates an infinite stream by repeatedly applying a function to a seed value.

Callers

nothing calls this directly

Calls 6

fromPullFunction · 0.70
loopFunction · 0.70
fFunction · 0.50
syncMethod · 0.45
doneMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected