Hyperlinkv0.8.0-beta.28

Stream

Stream.runForEachconsteffect/Stream.ts:10866
<A, X, E2, R2>(f: (a: A) => Effect.Effect<X, E2, R2>): <E, R>(
  self: Stream<A, E, R>
) => Effect.Effect<void, E2 | E, R2 | R>
<A, E, R, X, E2, R2>(
  self: Stream<A, E, R>,
  f: (a: A) => Effect.Effect<X, E2, R2>
): Effect.Effect<void, E | E2, R | R2>

Runs the provided effectful callback for each element of the stream.

Example (Running an effect for each value)

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

const stream = Stream.make(1, 2, 3)

const program = Effect.gen(function*() {
  yield* Stream.runForEach(stream, (n) => Console.log(`Processing: ${n}`))
})

Effect.runPromise(program)
// Processing: 1
// Processing: 2
// Processing: 3
destructors
Source effect/Stream.ts:1086620 lines
export const runForEach: {
  <A, X, E2, R2>(
    f: (a: A) => Effect.Effect<X, E2, R2>
  ): <E, R>(self: Stream<A, E, R>) => Effect.Effect<void, E2 | E, R2 | R>
  <A, E, R, X, E2, R2>(
    self: Stream<A, E, R>,
    f: (a: A) => Effect.Effect<X, E2, R2>
  ): Effect.Effect<void, E | E2, R | R2>
} = dual(2, <A, E, R, X, E2, R2>(
  self: Stream<A, E, R>,
  f: (a: A) => Effect.Effect<X, E2, R2>
): Effect.Effect<void, E | E2, R | R2> =>
  Channel.runForEach(self.channel, (arr) => {
    let i = 0
    return Effect.whileLoop({
      while: () => i < arr.length,
      body: () => f(arr[i++]),
      step: constVoid
    })
  }))
Referenced by 3 symbols