Hyperlinkv0.8.0-beta.28

Channel

Channel.scanconsteffect/Channel.ts:3723
<S, OutElem>(initial: S, f: (s: S, a: Types.NoInfer<OutElem>) => S): <
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env
>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
) => Channel<S, OutErr, OutDone, InElem, InErr, InDone, Env>
<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, S>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  initial: S,
  f: (s: S, a: Types.NoInfer<OutElem>) => S
): Channel<S, OutErr, OutDone, InElem, InErr, InDone, Env>

Transforms a channel statefully by scanning over its output with an accumulator function. Emits the intermediate results of the scan operation.

Example (Scanning channel output)

import { Channel } from "effect"

// Create a channel with numbers
const numbersChannel = Channel.fromIterable([1, 2, 3, 4, 5])

// Scan to create running sum
const runningSumChannel = Channel.scan(numbersChannel, 0, (sum, n) => sum + n)
// Outputs: 0, 1, 3, 6, 10, 15
// Note: emits the initial value and each intermediate result

// Scan with string concatenation
const wordsChannel = Channel.fromIterable(["hello", "world", "from", "effect"])
const sentenceChannel = Channel.scan(
  wordsChannel,
  "",
  (sentence, word) => sentence === "" ? word : `${sentence} ${word}`
)
// Outputs: "", "hello", "hello world", "hello world from", "hello world from effect"
sequencing
Source effect/Channel.ts:372328 lines
export const scan: {
  <S, OutElem>(initial: S, f: (s: S, a: Types.NoInfer<OutElem>) => S): <
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >(self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>) => Channel<
    S,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, S>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    initial: S,
    f: (s: S, a: Types.NoInfer<OutElem>) => S
  ): Channel<S, OutErr, OutDone, InElem, InErr, InDone, Env>
} = dual(3, <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, S>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  initial: S,
  f: (s: S, a: Types.NoInfer<OutElem>) => S
): Channel<S, OutErr, OutDone, InElem, InErr, InDone, Env> =>
  scanEffect(self, initial, (s, a) => Effect.succeed(f(s, a))))