Hyperlinkv0.8.0-beta.28

Stream

Stream.scanEffectconsteffect/Stream.ts:7793
<S, A, E2, R2>(initial: S, f: (s: S, a: A) => Effect.Effect<S, E2, R2>): <
  E,
  R
>(
  self: Stream<A, E, R>
) => Stream<S, E | E2, R | R2>
<A, E, R, S, E2, R2>(
  self: Stream<A, E, R>,
  initial: S,
  f: (s: S, a: A) => Effect.Effect<S, E2, R2>
): Stream<S, E | E2, R | R2>

Accumulates state effectfully and emits the initial state plus each accumulated state.

Example (Effectfully scanning stream state)

import { Console, Effect, Stream } from "effect"

const program = Effect.gen(function*() {
  const states = yield* Stream.make(1, 2, 3).pipe(
    Stream.scanEffect(0, (sum, n) => Effect.succeed(sum + n)),
    Stream.runCollect
  )
  yield* Console.log(states)
  // Output: [ 0, 1, 3, 6 ]
})
Accumulation
Source effect/Stream.ts:779321 lines
export const scanEffect: {
  <S, A, E2, R2>(
    initial: S,
    f: (s: S, a: A) => Effect.Effect<S, E2, R2>
  ): <E, R>(self: Stream<A, E, R>) => Stream<S, E | E2, R | R2>
  <A, E, R, S, E2, R2>(
    self: Stream<A, E, R>,
    initial: S,
    f: (s: S, a: A) => Effect.Effect<S, E2, R2>
  ): Stream<S, E | E2, R | R2>
} = dual(3, <A, E, R, S, E2, R2>(
  self: Stream<A, E, R>,
  initial: S,
  f: (s: S, a: A) => Effect.Effect<S, E2, R2>
): Stream<S, E | E2, R | R2> =>
  self.channel.pipe(
    Channel.flattenArray,
    Channel.scanEffect(initial, f),
    Channel.map(Arr.of),
    fromChannel
  ))