<A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<
A,
E | E2,
R2 | Exclude<R, Scope.Scope>
>Creates a stream produced from an Effect.
Example (Unwrapping a stream effect)
import { Console, Effect, Stream } from "effect"
const effect = Effect.succeed(Stream.make(1, 2, 3))
const stream = Stream.unwrap(effect)
const program = Effect.gen(function*() {
const chunk = yield* Stream.runCollect(stream)
yield* Console.log(chunk)
})
// [1, 2, 3]export const const unwrap: <A, E2, R2, E, R>(
effect: Effect.Effect<Stream<A, E2, R2>, E, R>
) => Stream<
A,
E | E2,
R2 | Exclude<R, Scope.Scope>
>
Creates a stream produced from an Effect.
Example (Unwrapping a stream effect)
import { Console, Effect, Stream } from "effect"
const effect = Effect.succeed(Stream.make(1, 2, 3))
const stream = Stream.unwrap(effect)
const program = Effect.gen(function*() {
const chunk = yield* Stream.runCollect(stream)
yield* Console.log(chunk)
})
// [1, 2, 3]
unwrap = <function (type parameter) A in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>A, function (type parameter) E2 in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>E2, function (type parameter) R2 in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>R2, function (type parameter) E in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>E, function (type parameter) R in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>R>(
effect: Effect.Effect<Stream<A, E2, R2>, E, R>(parameter) effect: {
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; <…;
toString: () => string;
toJSON: () => unknown;
}
effect: import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<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, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>A, function (type parameter) E2 in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>E2, function (type parameter) R2 in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>R2>, function (type parameter) E in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>E, function (type parameter) R in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>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<function (type parameter) A in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>A, function (type parameter) E in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>E | function (type parameter) E2 in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>E2, function (type parameter) R2 in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>R2 | type Exclude<T, U> = T extends U
? never
: T
Exclude from T those types that are assignable to U
Exclude<function (type parameter) R in <A, E2, R2, E, R>(effect: Effect.Effect<Stream<A, E2, R2>, E, R>): Stream<A, E | E2, R2 | Exclude<R, Scope.Scope>>R, import ScopeScope.Scope>> => const fromChannel: <
Arr extends Arr.NonEmptyReadonlyArray<any>,
E,
R
>(
channel: Channel.Channel<
Arr,
E,
void,
unknown,
unknown,
unknown,
R
>
) => Stream<
Arr extends Arr.NonEmptyReadonlyArray<infer A>
? A
: never,
E,
R
>
Creates a stream from a array-emitting Channel.
Example (Creating a stream from an array-emitting channel)
import { Channel, Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const channel = Channel.succeed([1, 2, 3] as const)
const stream = Stream.fromChannel(channel)
const result = yield* Stream.runCollect(stream)
yield* Console.log(result)
})
// Output: [ 1, 2, 3 ]
fromChannel(import ChannelChannel.const unwrap: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
R2,
E,
R
>(
channel: Effect.Effect<
Channel<
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
R2
>,
E,
R
>
) => Channel<
OutElem,
E | OutErr,
OutDone,
InElem,
InErr,
InDone,
Exclude<R, Scope.Scope> | R2
>
Constructs a Channel from a scoped effect that will result in a
Channel if successful.
Example (Unwrapping channel effects)
import { Channel, Data, Effect } from "effect"
class UnwrapError extends Data.TaggedError("UnwrapError")<{
readonly reason: string
}> {}
// Create an effect that produces a channel
const channelEffect = Effect.succeed(
Channel.fromIterable([1, 2, 3])
)
// Unwrap the effect to get the channel
const unwrappedChannel = Channel.unwrap(channelEffect)
// The resulting channel outputs: 1, 2, 3
unwrap(import EffectEffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E, R>
<A, E, R, B>(
self: Effect<A, E, R>,
f: (a: A) => B
): Effect<B, E, R>
}
map(effect: Effect.Effect<Stream<A, E2, R2>, E, R>(parameter) effect: {
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; <…;
toString: () => string;
toJSON: () => unknown;
}
effect, const toChannel: <A, E, R>(
stream: Stream<A, E, R>
) => Channel.Channel<
Arr.NonEmptyReadonlyArray<A>,
E,
void,
unknown,
unknown,
unknown,
R
>
Creates a channel from a stream.
Example (Converting a stream to a channel)
import { Channel, Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const stream = Stream.make(1, 2, 3)
const channel = Stream.toChannel(stream)
const values = yield* Channel.runCollect(channel)
yield* Console.log(values.flat())
})
Effect.runPromise(program)
// Output: [ 1, 2, 3 ]
toChannel)))