<In, X, E, R>(
f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>
): Sink<void, In, never, E, R>A sink that executes the provided effectful function for every Chunk fed to it.
Example (Running effects for each chunk)
import { Console, Effect, Sink, Stream } from "effect"
// Create a sink that processes chunks
const sink = Sink.forEachArray((chunk: ReadonlyArray<number>) =>
Console.log(
`Processing chunk of ${chunk.length} items: [${chunk.join(", ")}]`
)
)
// Use it with a stream
const stream = Stream.make(1, 2, 3, 4, 5)
const program = Stream.run(stream, sink)
Effect.runPromise(program)
// Output: Processing chunk of 5 items: [1, 2, 3, 4, 5]export const const forEachArray: <In, X, E, R>(
f: (
input: NonEmptyReadonlyArray<In>
) => Effect.Effect<X, E, R>
) => Sink<void, In, never, E, R>
A sink that executes the provided effectful function for every Chunk fed
to it.
Example (Running effects for each chunk)
import { Console, Effect, Sink, Stream } from "effect"
// Create a sink that processes chunks
const sink = Sink.forEachArray((chunk: ReadonlyArray<number>) =>
Console.log(
`Processing chunk of ${chunk.length} items: [${chunk.join(", ")}]`
)
)
// Use it with a stream
const stream = Stream.make(1, 2, 3, 4, 5)
const program = Stream.run(stream, sink)
Effect.runPromise(program)
// Output: Processing chunk of 5 items: [1, 2, 3, 4, 5]
forEachArray = <function (type parameter) In in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>In, function (type parameter) X in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>X, function (type parameter) E in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>E, function (type parameter) R in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>R>(
f: (
input: NonEmptyReadonlyArray<In>
) => Effect.Effect<X, E, R>
f: (input: NonEmptyReadonlyArray<In>(parameter) input: {
0: In;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<In>>): Array<In>; (...items: Array<In | ConcatArray<In>>): Array<In> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<In>;
indexOf: (searchElement: In, fromIndex?: number) => number;
lastIndexOf: (searchElement: In, fromIndex?: number) => number;
every: { (predicate: (value: In, index: number, array: ReadonlyArray<In>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: In, index: number, array: ReadonlyArray<In>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: In, index: number, array: ReadonlyArray<In>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: In, index: number, array: ReadonlyArray<In>) => void, thisArg?: any) => void;
map: (callbackfn: (value: In, index: number, array: ReadonlyArray<In>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: In, index: number, array: ReadonlyArray<In>) => value is S, thisArg?: any): Array<S>; (predicate: (value: In, index: number, array: ReadonlyArray<In>) => unknown, thisArg?: any): Array<In> };
reduce: { (callbackfn: (previousValue: In, currentValue: In, currentIndex: number, array: ReadonlyArray<In>) => In): In; (callbackfn: (previousValue: In, currentValue: In, currentIndex: number, array: ReadonlyArray<In>) => In, initialValue: In): I…;
reduceRight: { (callbackfn: (previousValue: In, currentValue: In, currentIndex: number, array: ReadonlyArray<In>) => In): In; (callbackfn: (previousValue: In, currentValue: In, currentIndex: number, array: ReadonlyArray<In>) => In, initialValue: In): I…;
find: { (predicate: (value: In, index: number, obj: ReadonlyArray<In>) => value is S, thisArg?: any): S | undefined; (predicate: (value: In, index: number, obj: ReadonlyArray<In>) => unknown, thisArg?: any): In | undefined };
findIndex: (predicate: (value: In, index: number, obj: ReadonlyArray<In>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, In]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<In>;
includes: (searchElement: In, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: In, index: number, array: Array<In>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => In | undefined;
findLast: { (predicate: (value: In, index: number, array: ReadonlyArray<In>) => value is S, thisArg?: any): S | undefined; (predicate: (value: In, index: number, array: ReadonlyArray<In>) => unknown, thisArg?: any): In | undefined };
findLastIndex: (predicate: (value: In, index: number, array: ReadonlyArray<In>) => unknown, thisArg?: any) => number;
toReversed: () => Array<In>;
toSorted: (compareFn?: ((a: In, b: In) => number) | undefined) => Array<In>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<In>): Array<In>; (start: number, deleteCount?: number): Array<In> };
with: (index: number, value: In) => Array<In>;
}
input: type NonEmptyReadonlyArray<A> = readonly [A, ...A[]]A readonly array guaranteed to have at least one element.
When to use
Use when non-emptiness must be tracked at the type level while preventing mutation.
Many Array module functions accept or return this type.
Example (Typing a non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyReadonlyArray<number> = [1, 2, 3]
const head: number = nonEmpty[0] // guaranteed to exist
NonEmptyReadonlyArray<function (type parameter) In in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>In>) => 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<function (type parameter) X in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>X, function (type parameter) E in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>E, function (type parameter) R in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>R>
): interface Sink<out A, in In = unknown, out L = never, out E = never, out R = never>A Sink<A, In, L, E, R> is used to consume elements produced by a Stream.
You can think of a sink as a function that will consume a variable amount of
In elements (could be 0, 1, or many), might fail with an error of type E,
and will eventually yield a value of type A together with a remainder of
type L (i.e. any leftovers).
Example (Running a sink with a stream)
import { Effect, Sink, Stream } from "effect"
// Create a simple sink that always succeeds with a value
const sink: Sink.Sink<number> = Sink.succeed(42)
// Use the sink to consume a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)
Effect.runPromise(program).then(console.log)
// Output: 42
Namespace containing types and interfaces for Sink variance and type relationships.
Sink<void, function (type parameter) In in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>In, never, function (type parameter) E in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>E, function (type parameter) R in <In, X, E, R>(f: (input: NonEmptyReadonlyArray<In>) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>R> =>
const fromTransform: <
In,
A,
E,
R,
L = never
>(
transform: (
upstream: Pull.Pull<
NonEmptyReadonlyArray<In>,
never,
void
>,
scope: Scope.Scope
) => Effect.Effect<End<A, L>, E, R>
) => Sink<A, In, L, E, R>
Creates a Sink from a low-level transform function.
Details
The transform receives the upstream pull of non-empty input arrays and the
active scope, and returns an effect that completes with the sink's End
value.
fromTransform((upstream: Pull.Pull<
readonly [In, ...In[]],
never,
void,
never
>
(parameter) upstream: {
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;
}
upstream) =>
upstream: Pull.Pull<
readonly [In, ...In[]],
never,
void,
never
>
(parameter) upstream: {
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;
}
upstream.Pipeable.pipe<Pull.Pull<readonly [In, ...In[]], never, void, never>, Effect.Effect<X, Cause.Done<void> | E, R>, Effect.Effect<never, Cause.Done<void> | E, R>, Effect.Effect<End<void, never>, Exclude<E, Cause.Done<any>>, R>>(this: Pull.Pull<...>, ab: (_: Pull.Pull<readonly [In, ...In[]], never, void, never>) => Effect.Effect<X, Cause.Done<void> | E, R>, bc: (_: Effect.Effect<X, Cause.Done<void> | E, R>) => Effect.Effect<never, Cause.Done<...> | E, R>, cd: (_: Effect.Effect<...>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)pipe(
import EffectEffect.const flatMap: {
<A, B, E1, R1>(
f: (a: A) => Effect<B, E1, R1>
): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E1 | E, R1 | R>
<A, E, R, B, E1, R1>(
self: Effect<A, E, R>,
f: (a: A) => Effect<B, E1, R1>
): Effect<B, E | E1, R | R1>
}
flatMap(f: (
input: NonEmptyReadonlyArray<In>
) => Effect.Effect<X, E, R>
f),
import EffectEffect.const forever: <
Arg extends
| Effect<any, any, any>
| {
readonly disableYield?:
| boolean
| undefined
}
| undefined = {
readonly disableYield?: boolean | undefined
}
>(
effectOrOptions?: Arg,
options?:
| {
readonly disableYield?:
| boolean
| undefined
}
| undefined
) => [Arg] extends [
Effect<infer _A, infer _E, infer _R>
]
? Effect<never, _E, _R>
: <A, E, R>(
self: Effect<A, E, R>
) => Effect<never, E, R>
Repeats this effect forever (until the first error).
Example (Repeating forever)
import { Console, Effect, Fiber } from "effect"
const task = Effect.gen(function*() {
yield* Console.log("Task running...")
yield* Effect.sleep("1 second")
})
// This will run forever, printing every second
const program = task.pipe(Effect.forever)
// This will run forever, without yielding every iteration
const programNoYield = task.pipe(Effect.forever({ disableYield: true }))
// Run for 5 seconds then interrupt
const timedProgram = Effect.gen(function*() {
const fiber = yield* Effect.forkChild(program)
yield* Effect.sleep("5 seconds")
yield* Fiber.interrupt(fiber)
})
forever({ disableYield: truedisableYield: true }),
import PullPull.const catchDone: {
<E, A2, E2, R2>(
f: (
leftover: Cause.Done.Extract<E>
) => Effect<A2, E2, R2>
): <A, R>(
self: Effect<A, E, R>
) => Effect<A | A2, ExcludeDone<E> | E2, R | R2>
<A, R, E, A2, E2, R2>(
self: Effect<A, E, R>,
f: (
leftover: Cause.Done.Extract<E>
) => Effect<A2, E2, R2>
): Effect<A | A2, ExcludeDone<E> | E2, R | R2>
}
catchDone(() => const endVoid: Effect.Effect<
End<void, never>,
never,
never
>
const endVoid: {
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;
}
endVoid)
)
)