<A>(value: A, next: (value: A) => A): Stream<A>Creates an infinite stream by repeatedly applying a function to a seed value.
Example (Iterating from a seed value)
import { Console, Effect, Stream } from "effect"
const stream = Stream.iterate(1, (n) => n + 1).pipe(Stream.take(3))
const program = Effect.gen(function* () {
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 1, 2, 3 ]export const const iterate: <A>(
value: A,
next: (value: A) => A
) => Stream<A>
Creates an infinite stream by repeatedly applying a function to a seed value.
Example (Iterating from a seed value)
import { Console, Effect, Stream } from "effect"
const stream = Stream.iterate(1, (n) => n + 1).pipe(Stream.take(3))
const program = Effect.gen(function* () {
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 1, 2, 3 ]
iterate = <function (type parameter) A in <A>(value: A, next: (value: A) => A): Stream<A>A>(value: Avalue: function (type parameter) A in <A>(value: A, next: (value: A) => A): Stream<A>A, next: (value: A) => Anext: (value: Avalue: function (type parameter) A in <A>(value: A, next: (value: A) => A): Stream<A>A) => function (type parameter) A in <A>(value: A, next: (value: A) => A): Stream<A>A): interface Stream<out A, out E = never, out R = never>A Stream<A, E, R> describes a program that can emit many A values, fail
with E, and require R.
Details
Streams are pull-based with backpressure and emit chunks to amortize effect
evaluation. They support monadic composition and error handling similar to
Effect, adapted for multiple values.
Example (Creating and consuming streams)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.make(1, 2, 3).pipe(
Stream.map((n) => n * 2),
Stream.runForEach((n) => Console.log(n))
)
})
Effect.runPromise(program)
// Output:
// 2
// 4
// 6
Stream<function (type parameter) A in <A>(value: A, next: (value: A) => A): Stream<A>A> =>
const unfold: <S, A, E, R>(
s: S,
f: (
s: S
) => Effect.Effect<
readonly [A, S] | undefined,
E,
R
>
) => Stream<A, E, R>
Creates a stream by repeatedly applying an effectful step function to a
state.
Details
Each readonly [value, nextState] result emits value and continues with
nextState; returning undefined ends the stream.
Example (Unfolding stream state)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const stream = Stream.unfold(1, (n) => Effect.succeed([n, n + 1] as const))
const values = yield* Stream.runCollect(stream.pipe(Stream.take(5)))
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 1, 2, 3, 4, 5 ]
unfold(value: Avalue, (a: Aa) => import EffectEffect.succeed([a: Aa, next: (value: A) => Anext(a: Aa)]))