<A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>Runs the stream and returns the first element as an Option.
Example (Getting the first stream value)
import { Console, Effect, Option, Stream } from "effect"
const program = Effect.gen(function*() {
const head = yield* Stream.runHead(Stream.make(1, 2, 3))
yield* Console.log(Option.getOrThrow(head))
})
Effect.runPromise(program)
// 1export const const runHead: <A, E, R>(
self: Stream<A, E, R>
) => Effect.Effect<Option.Option<A>, E, R>
Runs the stream and returns the first element as an Option.
Example (Getting the first stream value)
import { Console, Effect, Option, Stream } from "effect"
const program = Effect.gen(function*() {
const head = yield* Stream.runHead(Stream.make(1, 2, 3))
yield* Console.log(Option.getOrThrow(head))
})
Effect.runPromise(program)
// 1
runHead = <function (type parameter) A in <A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>A, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>E, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<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>): Effect.Effect<Option.Option<A>, E, R>A, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>E, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>R>): 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<import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in <A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>A>, function (type parameter) E in <A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>E, function (type parameter) R in <A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>R> =>
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(import ChannelChannel.const runHead: <
OutElem,
OutErr,
OutDone,
Env
>(
self: Channel<
OutElem,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
) => Effect.Effect<
Option.Option<OutElem>,
OutErr,
Env
>
Runs a channel until the first output element is available, returning it in
an Option.
Details
Returns Option.some with the first output element, or Option.none if the
channel completes without emitting output.
runHead(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.Stream<A, E, R>.channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>(property) Stream<A, E, R>.channel: {
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; <…;
}
channel), import OptionOption.const map: {
<A, B>(f: (a: A) => B): (
self: Option<A>
) => Option<B>
<A, B>(
self: Option<A>,
f: (a: A) => B
): Option<B>
}
map(import ArrArr.const getUnsafe: (index: number) => <A>(self: ReadonlyArray<A>) => A (+1 overload)getUnsafe(0)))