<A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>Collects all elements into an array and emits it as a single element.
Example (Collecting values into a stream element)
import { Console, Effect, Stream } from "effect"
const stream = Stream.make(1, 2, 3)
const program = Effect.gen(function*() {
const collected = yield* stream.pipe(Stream.collect, Stream.runCollect)
yield* Console.log(collected[0])
})
Effect.runPromise(program)
// [1, 2, 3]export const const collect: <A, E, R>(
self: Stream<A, E, R>
) => Stream<Array<A>, E, R>
Collects all elements into an array and emits it as a single element.
Example (Collecting values into a stream element)
import { Console, Effect, Stream } from "effect"
const stream = Stream.make(1, 2, 3)
const program = Effect.gen(function*() {
const collected = yield* stream.pipe(Stream.collect, Stream.runCollect)
yield* Console.log(collected[0])
})
Effect.runPromise(program)
// [1, 2, 3]
collect = <function (type parameter) A in <A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>A, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>E, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>R>(self: Stream<A, E, R>(parameter) self: {
channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self: 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, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>A, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>E, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>R>): 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<interface Array<T>Array<function (type parameter) A in <A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>A>, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>E, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Stream<Array<A>, E, R>R> => const fromEffect: <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Stream<A, E, R>
Creates a stream from an effect.
Example (Creating a stream from an effect)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const stream = Stream.fromEffect(Effect.succeed(42))
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 42 ]
fromEffect(const runCollect: <A, E, R>(
self: Stream<A, E, R>
) => Effect.Effect<Array<A>, E, R>
Runs the stream and collects all elements into an array.
Example (Collecting stream values)
import { Console, Effect, Stream } from "effect"
const stream = Stream.make(1, 2, 3, 4, 5)
const program = Effect.gen(function*() {
const collected = yield* Stream.runCollect(stream)
yield* Console.log(collected)
})
Effect.runPromise(program)
// [1, 2, 3, 4, 5]
runCollect(self: Stream<A, E, R>(parameter) self: {
channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self))