<A, E, R>(self: Stream<A, E, R>): Effect.Effect<Option.Option<A>, E, R>Runs the stream and returns the last element as an Option.
When to use
Use to consume a finite stream when only the final emitted element matters.
Details
Option.some contains the last emitted element. Option.none means the
stream completed without emitting.
Gotchas
The returned effect waits for the stream to complete before it can produce a value.
export const const runLast: <A, E, R>(
self: Stream<A, E, R>
) => Effect.Effect<Option.Option<A>, E, R>
Runs the stream and returns the last element as an Option.
When to use
Use to consume a finite stream when only the final emitted element matters.
Details
Option.some contains the last emitted element. Option.none means the
stream completed without emitting.
Gotchas
The returned effect waits for the stream to complete before it can produce a
value.
runLast = <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 runLast: <
OutElem,
OutErr,
OutDone,
Env
>(
self: Channel<
OutElem,
OutErr,
OutDone,
unknown,
unknown,
unknown,
Env
>
) => Effect.Effect<
Option.Option<OutElem>,
OutErr,
Env
>
Runs a channel to completion and returns the last output element in an
Option.
Details
Returns Option.some with the last emitted element, or Option.none if the
channel completes without emitting output.
runLast(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 lastNonEmpty: <A>(
self: NonEmptyReadonlyArray<A>
) => A
Returns the last element of a NonEmptyReadonlyArray directly (no Option
wrapper).
When to use
Use to get the last element without Option wrapping when the array is known
to be non-empty.
Example (Getting the last of a non-empty array)
import { Array } from "effect"
console.log(Array.lastNonEmpty([1, 2, 3, 4])) // 4
lastNonEmpty))