<Err, Done>(): Channel<
Arr.NonEmptyReadonlyArray<string>,
Err,
Done,
Arr.NonEmptyReadonlyArray<string>,
Err,
Done
>Splits upstream string chunks into lines, recognizing \n, \r\n, and
standalone \r as line terminators. The behavior matches
String.linesIterator regardless of how the input is chunked.
Details
A line terminator at the very end of the stream does not produce a
trailing empty line (consistent with String.linesIterator). Conversely,
if the stream ends without a terminator the final partial line is still
emitted.
Example (Splitting string chunks into lines)
import { Effect, Stream } from "effect"
Effect.runPromise(Effect.gen(function*() {
const result = yield* Stream.runCollect(
Stream.splitLines(Stream.make("hel", "lo\r\nwor", "ld\n"))
)
console.log(result)
// [ 'hello', 'world' ]
}))export const const splitLines: <
Err,
Done
>() => Channel<
Arr.NonEmptyReadonlyArray<string>,
Err,
Done,
Arr.NonEmptyReadonlyArray<string>,
Err,
Done
>
Splits upstream string chunks into lines, recognizing \n, \r\n, and
standalone \r as line terminators. The behavior matches
String.linesIterator regardless of how the input is chunked.
Details
A line terminator at the very end of the stream does not produce a
trailing empty line (consistent with String.linesIterator). Conversely,
if the stream ends without a terminator the final partial line is still
emitted.
Example (Splitting string chunks into lines)
import { Effect, Stream } from "effect"
Effect.runPromise(Effect.gen(function*() {
const result = yield* Stream.runCollect(
Stream.splitLines(Stream.make("hel", "lo\r\nwor", "ld\n"))
)
console.log(result)
// [ 'hello', 'world' ]
}))
splitLines = <function (type parameter) Err in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Err, function (type parameter) Done in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Done>(): interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<
import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string>,
function (type parameter) Err in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Err,
function (type parameter) Done in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Done,
import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string>,
function (type parameter) Err in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Err,
function (type parameter) Done in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Done
> =>
const fromTransform: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
EX,
EnvX,
Env
>(
transform: (
upstream: Pull.Pull<InElem, InErr, InDone>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
InElem,
InErr,
InDone,
Env | EnvX
>
Creates a Channel from a transformation function that operates on upstream pulls.
Example (Creating channels from transforms)
import { Channel, Effect } from "effect"
const channel = Channel.fromTransform((upstream, scope) =>
Effect.succeed(upstream)
)
fromTransform((upstream: Pull.Pull<
readonly [string, ...string[]],
Err,
Done,
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, _scope: Scope.Scope(parameter) _scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
_scope) =>
import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() => {
// Accumulates text that has not yet been terminated by a line break.
// Content is carried across chunks until a terminator is found.
let let stringBuilder: stringstringBuilder = ""
// Set when a chunk ends with \r so the next chunk can check whether
// the following character is \n (completing a \r\n pair) or not
// (standalone \r, which is itself a line terminator).
let let midCRLF: booleanmidCRLF = false
// Remembers the upstream Done value after the first time the upstream
// signals completion, so subsequent pulls return Done immediately
// without pulling upstream again.
let let done: Option.Option<Done>done = import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none<function (type parameter) Done in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Done>()
function function (local function) splitLinesArray(chunk: Arr.NonEmptyReadonlyArray<string>): Arr.NonEmptyReadonlyArray<string> | nullsplitLinesArray(chunk: Arr.NonEmptyReadonlyArray<string>(parameter) chunk: {
0: string;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<string>>): Array<string>; (...items: Array<string | ConcatArray<string>>): Array<string> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<string>;
indexOf: (searchElement: string, fromIndex?: number) => number;
lastIndexOf: (searchElement: string, fromIndex?: number) => number;
every: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => void, thisArg?: any) => void;
map: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): Array<S>; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): Array<string> };
reduce: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
reduceRight: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
find: { (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefined };
findIndex: (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, string]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<string>;
includes: (searchElement: string, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: string, index: number, array: Array<string>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => string | undefined;
findLast: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefine…;
findLastIndex: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
toReversed: () => Array<string>;
toSorted: (compareFn?: ((a: string, b: string) => number) | undefined) => Array<string>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<string>): Array<string>; (start: number, deleteCount?: number): Array<string> };
with: (index: number, value: string) => Array<string>;
}
chunk: import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string>): import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string> | null {
const const chunkBuilder: string[]chunkBuilder: interface Array<T>Array<string> = []
function function (local function) pushLine(segment: string): voidpushLine(segment: stringsegment: string): void {
if (let stringBuilder: stringstringBuilder.globalThis.String.length: numberReturns the length of a String object.
length === 0) {
const chunkBuilder: string[]chunkBuilder.Array<string>.push(...items: string[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(segment: stringsegment)
} else {
const chunkBuilder: string[]chunkBuilder.Array<string>.push(...items: string[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(let stringBuilder: stringstringBuilder + segment: stringsegment)
let stringBuilder: stringstringBuilder = ""
}
}
for (let let i: numberi = 0; let i: numberi < chunk: Arr.NonEmptyReadonlyArray<string>(parameter) chunk: {
0: string;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<string>>): Array<string>; (...items: Array<string | ConcatArray<string>>): Array<string> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<string>;
indexOf: (searchElement: string, fromIndex?: number) => number;
lastIndexOf: (searchElement: string, fromIndex?: number) => number;
every: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => void, thisArg?: any) => void;
map: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): Array<S>; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): Array<string> };
reduce: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
reduceRight: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
find: { (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefined };
findIndex: (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, string]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<string>;
includes: (searchElement: string, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: string, index: number, array: Array<string>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => string | undefined;
findLast: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefine…;
findLastIndex: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
toReversed: () => Array<string>;
toSorted: (compareFn?: ((a: string, b: string) => number) | undefined) => Array<string>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<string>): Array<string>; (start: number, deleteCount?: number): Array<string> };
with: (index: number, value: string) => Array<string>;
}
chunk.length; let i: numberi++) {
const const str: Arr.NonEmptyReadonlyArray<string>str = chunk: Arr.NonEmptyReadonlyArray<string>(parameter) chunk: {
0: string;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<string>>): Array<string>; (...items: Array<string | ConcatArray<string>>): Array<string> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<string>;
indexOf: (searchElement: string, fromIndex?: number) => number;
lastIndexOf: (searchElement: string, fromIndex?: number) => number;
every: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => void, thisArg?: any) => void;
map: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): Array<S>; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): Array<string> };
reduce: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
reduceRight: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
find: { (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefined };
findIndex: (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, string]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<string>;
includes: (searchElement: string, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: string, index: number, array: Array<string>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => string | undefined;
findLast: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefine…;
findLastIndex: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
toReversed: () => Array<string>;
toSorted: (compareFn?: ((a: string, b: string) => number) | undefined) => Array<string>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<string>): Array<string>; (start: number, deleteCount?: number): Array<string> };
with: (index: number, value: string) => Array<string>;
}
chunk[let i: numberi]
if (const str: Arr.NonEmptyReadonlyArray<string>str.length !== 0) {
let let from: numberfrom = 0
let let indexOfCR: anyindexOfCR = const str: Arr.NonEmptyReadonlyArray<string>str.indexOf("\r")
let let indexOfLF: anyindexOfLF = const str: Arr.NonEmptyReadonlyArray<string>str.indexOf("\n")
if (let midCRLF: booleanmidCRLF) {
if (let indexOfLF: anyindexOfLF === 0) {
function (local function) pushLine(segment: string): voidpushLine("")
let from: numberfrom = 1
let indexOfLF: anyindexOfLF = const str: Arr.NonEmptyReadonlyArray<string>str.indexOf("\n", let from: numberfrom)
} else {
function (local function) pushLine(segment: string): voidpushLine("")
}
let midCRLF: booleanmidCRLF = false
}
while (let indexOfCR: anyindexOfCR !== -1 || let indexOfLF: anyindexOfLF !== -1) {
if (let indexOfCR: anyindexOfCR === -1 || (let indexOfLF: anyindexOfLF !== -1 && let indexOfLF: anyindexOfLF < let indexOfCR: anyindexOfCR)) {
function (local function) pushLine(segment: string): voidpushLine(const str: Arr.NonEmptyReadonlyArray<string>str.substring(let from: numberfrom, let indexOfLF: anyindexOfLF))
let from: numberfrom = let indexOfLF: anyindexOfLF + 1
let indexOfLF: anyindexOfLF = const str: Arr.NonEmptyReadonlyArray<string>str.indexOf("\n", let from: numberfrom)
} else {
if (const str: Arr.NonEmptyReadonlyArray<string>str.length === let indexOfCR: anyindexOfCR + 1) {
let midCRLF: booleanmidCRLF = true
let indexOfCR: anyindexOfCR = -1
} else {
function (local function) pushLine(segment: string): voidpushLine(const str: Arr.NonEmptyReadonlyArray<string>str.substring(let from: numberfrom, let indexOfCR: anyindexOfCR))
let from: numberfrom = let indexOfCR: anyindexOfCR + (let indexOfLF: anyindexOfLF === let indexOfCR: anyindexOfCR + 1 ? 2 : 1)
let indexOfCR: anyindexOfCR = const str: Arr.NonEmptyReadonlyArray<string>str.indexOf("\r", let from: numberfrom)
let indexOfLF: anyindexOfLF = const str: Arr.NonEmptyReadonlyArray<string>str.indexOf("\n", let from: numberfrom)
}
}
}
let stringBuilder: stringstringBuilder = let stringBuilder: stringstringBuilder + const str: Arr.NonEmptyReadonlyArray<string>str.substring(let from: numberfrom, const str: Arr.NonEmptyReadonlyArray<string>str.length - (let midCRLF: booleanmidCRLF ? 1 : 0))
}
}
return import ArrArr.isReadonlyArrayNonEmpty(const chunkBuilder: string[]chunkBuilder) ? const chunkBuilder: string[]chunkBuilder : null
}
const const pullOrFlush: Pull.Pull<
Arr.NonEmptyReadonlyArray<string>,
Err,
Done
>
const pullOrFlush: {
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;
}
pullOrFlush: import PullPull.interface Pull<out A, out E = never, out Done = void, out R = never>An effectful pull step that either produces a value, fails with E, or
signals completion with Cause.Done<Done>.
When to use
Use to model one low-level pull step when a consumer repeatedly evaluates an
effect that may emit a value, fail normally, or signal normal completion
through Cause.Done.
Details
Pull represents completion in the error channel so low-level stream
consumers can distinguish ordinary failures from end-of-input and carry a
leftover value when needed.
Pull<import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string>, function (type parameter) Err in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Err, function (type parameter) Done in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Done> = import EffectEffect.const suspend: <A, E, R>(
effect: LazyArg<Effect<A, E, R>>
) => Effect<A, E, R>
Creates an Effect lazily, delaying construction until it is needed.
When to use
Use when you need to defer the evaluation of an effect until it is required.
Details
suspend takes a thunk that represents an effect and delays creating it
until the suspended effect is evaluated. This is useful for optimizing
expensive computations, managing circular dependencies such as recursive
functions, and helping TypeScript unify return types when branches construct
different effects. Any side effects or scoped captures inside the thunk are
re-executed on each invocation.
Example (Lazily evaluating side effects)
import { Effect } from "effect"
let i = 0
const bad = Effect.succeed(i++)
const good = Effect.suspend(() => Effect.succeed(i++))
console.log(Effect.runSync(bad)) // Output: 0
console.log(Effect.runSync(bad)) // Output: 0
console.log(Effect.runSync(good)) // Output: 1
console.log(Effect.runSync(good)) // Output: 2
Example (Suspending recursive Fibonacci evaluation)
import { Effect } from "effect"
const blowsUp = (n: number): Effect.Effect<number> =>
n < 2
? Effect.succeed(1)
: Effect.zipWith(blowsUp(n - 1), blowsUp(n - 2), (a, b) => a + b)
// console.log(Effect.runSync(blowsUp(32)))
// crash: JavaScript heap out of memory
const allGood = (n: number): Effect.Effect<number> =>
n < 2
? Effect.succeed(1)
: Effect.zipWith(
Effect.suspend(() => allGood(n - 1)),
Effect.suspend(() => allGood(n - 2)),
(a, b) => a + b
)
console.log(Effect.runSync(allGood(32)))
// Output: 3524578
Example (Helping TypeScript infer recursive effect types)
import { Effect } from "effect"
// Without suspend, TypeScript may struggle with type inference.
// Inferred type:
// (a: number, b: number) =>
// Effect<never, Error, never> | Effect<number, never, never>
const withoutSuspend = (a: number, b: number) =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
// Using suspend to unify return types.
// Inferred type:
// (a: number, b: number) => Effect<number, Error, never>
const withSuspend = (a: number, b: number) =>
Effect.suspend(() =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
)
suspend(() => {
if (let done: Option.Option<Done>done._tag: "None" | "Some"_tag === "Some") {
return import CauseCause.done(let done: Option.Some<Done>let done: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
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;
}
done.Some<Done>.value: Donevalue)
}
return import PullPull.const matchEffect: {
<
A,
E,
L,
AS,
ES,
RS,
AF,
EF,
RF,
AH,
EH,
RH
>(options: {
readonly onSuccess: (
value: A
) => Effect<AS, ES, RS>
readonly onFailure: (
failure: Cause.Cause<E>
) => Effect<AF, EF, RF>
readonly onDone: (
leftover: L
) => Effect<AH, EH, RH>
}): <R>(
self: Pull<A, E, L, R>
) => Effect<
AS | AF | AH,
ES | EF | EH,
R | RS | RF | RH
>
<
A,
E,
L,
R,
AS,
ES,
RS,
AF,
EF,
RF,
AH,
EH,
RH
>(
self: Pull<A, E, L, R>,
options: {
readonly onSuccess: (
value: A
) => Effect<AS, ES, RS>
readonly onFailure: (
failure: Cause.Cause<E>
) => Effect<AF, EF, RF>
readonly onDone: (
leftover: L
) => Effect<AH, EH, RH>
}
): Effect<
AS | AF | AH,
ES | EF | EH,
R | RS | RF | RH
>
}
matchEffect(upstream: Pull.Pull<
readonly [string, ...string[]],
Err,
Done,
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, {
onSuccess: (
chunk: Arr.NonEmptyReadonlyArray<string>
) => Pull.Pull<
Arr.NonEmptyReadonlyArray<string>,
Err,
Done
>
onSuccess: function (local function) loop(chunk: Arr.NonEmptyReadonlyArray<string>): Pull.Pull<Arr.NonEmptyReadonlyArray<string>, Err, Done>loop,
onFailure: <E>(
cause: Cause.Cause<E>
) => Effect<never, E>
onFailure: import EffectEffect.const failCause: <E>(
cause: Cause.Cause<E>
) => Effect<never, E>
Creates an Effect that represents a failure with a specific Cause.
When to use
Use when you already have a full Cause and need to preserve defects,
interruptions, annotations, or combined failures in the effect's failure
channel.
Details
This function allows you to create effects that fail with complex error
structures, including multiple errors, defects, interruptions, and more.
Example (Failing with a full Cause)
import { Cause, Effect } from "effect"
const program = Effect.failCause(
Cause.fail("Network error")
)
Effect.runPromiseExit(program).then(console.log)
// Output: { _id: 'Exit', _tag: 'Failure', cause: ... }
failCause,
onDone: (
leftover: Done
) => Effect.Effect<
Arr.NonEmptyReadonlyArray<string>,
any,
never
>
onDone: (leftover: Doneleftover) => {
let done: Option.Option<Done>done = import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(leftover: Doneleftover)
if (let stringBuilder: stringstringBuilder.globalThis.String.length: numberReturns the length of a String object.
length > 0 || let midCRLF: booleanmidCRLF) {
const const last: stringlast = let stringBuilder: stringstringBuilder
let stringBuilder: stringstringBuilder = ""
let midCRLF: booleanmidCRLF = false
return import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed([const last: stringlast] as import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string>)
}
return import CauseCause.done(leftover: Doneleftover)
}
})
})
function function (local function) loop(chunk: Arr.NonEmptyReadonlyArray<string>): Pull.Pull<Arr.NonEmptyReadonlyArray<string>, Err, Done>loop(chunk: Arr.NonEmptyReadonlyArray<string>(parameter) chunk: {
0: string;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<string>>): Array<string>; (...items: Array<string | ConcatArray<string>>): Array<string> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<string>;
indexOf: (searchElement: string, fromIndex?: number) => number;
lastIndexOf: (searchElement: string, fromIndex?: number) => number;
every: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => void, thisArg?: any) => void;
map: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): Array<S>; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): Array<string> };
reduce: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
reduceRight: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
find: { (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefined };
findIndex: (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, string]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<string>;
includes: (searchElement: string, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: string, index: number, array: Array<string>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => string | undefined;
findLast: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefine…;
findLastIndex: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
toReversed: () => Array<string>;
toSorted: (compareFn?: ((a: string, b: string) => number) | undefined) => Array<string>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<string>): Array<string>; (start: number, deleteCount?: number): Array<string> };
with: (index: number, value: string) => Array<string>;
}
chunk: import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string>): import PullPull.interface Pull<out A, out E = never, out Done = void, out R = never>An effectful pull step that either produces a value, fails with E, or
signals completion with Cause.Done<Done>.
When to use
Use to model one low-level pull step when a consumer repeatedly evaluates an
effect that may emit a value, fail normally, or signal normal completion
through Cause.Done.
Details
Pull represents completion in the error channel so low-level stream
consumers can distinguish ordinary failures from end-of-input and carry a
leftover value when needed.
Pull<import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string>, function (type parameter) Err in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Err, function (type parameter) Done in <Err, Done>(): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<string>, Err, Done>Done> {
const const lines: anylines = function (local function) splitLinesArray(chunk: Arr.NonEmptyReadonlyArray<string>): Arr.NonEmptyReadonlyArray<string> | nullsplitLinesArray(chunk: Arr.NonEmptyReadonlyArray<string>(parameter) chunk: {
0: string;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<string>>): Array<string>; (...items: Array<string | ConcatArray<string>>): Array<string> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<string>;
indexOf: (searchElement: string, fromIndex?: number) => number;
lastIndexOf: (searchElement: string, fromIndex?: number) => number;
every: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => void, thisArg?: any) => void;
map: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): Array<S>; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): Array<string> };
reduce: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
reduceRight: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
find: { (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefined };
findIndex: (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, string]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<string>;
includes: (searchElement: string, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: string, index: number, array: Array<string>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => string | undefined;
findLast: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefine…;
findLastIndex: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
toReversed: () => Array<string>;
toSorted: (compareFn?: ((a: string, b: string) => number) | undefined) => Array<string>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<string>): Array<string>; (start: number, deleteCount?: number): Array<string> };
with: (index: number, value: string) => Array<string>;
}
chunk)
return const lines: anylines !== null ? import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(const lines: anyconst lines: {
0: string;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<string>>): Array<string>; (...items: Array<string | ConcatArray<string>>): Array<string> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<string>;
indexOf: (searchElement: string, fromIndex?: number) => number;
lastIndexOf: (searchElement: string, fromIndex?: number) => number;
every: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => void, thisArg?: any) => void;
map: (callbackfn: (value: string, index: number, array: ReadonlyArray<string>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): Array<S>; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): Array<string> };
reduce: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
reduceRight: { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<string>) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: ReadonlyArray<stri…;
find: { (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefined };
findIndex: (predicate: (value: string, index: number, obj: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, string]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<string>;
includes: (searchElement: string, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: string, index: number, array: Array<string>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => string | undefined;
findLast: { (predicate: (value: string, index: number, array: ReadonlyArray<string>) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any): string | undefine…;
findLastIndex: (predicate: (value: string, index: number, array: ReadonlyArray<string>) => unknown, thisArg?: any) => number;
toReversed: () => Array<string>;
toSorted: (compareFn?: ((a: string, b: string) => number) | undefined) => Array<string>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<string>): Array<string>; (start: number, deleteCount?: number): Array<string> };
with: (index: number, value: string) => Array<string>;
}
lines) : const pullOrFlush: Pull.Pull<
Arr.NonEmptyReadonlyArray<string>,
Err,
Done
>
const pullOrFlush: {
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;
}
pullOrFlush
}
return const pullOrFlush: Pull.Pull<
Arr.NonEmptyReadonlyArray<string>,
Err,
Done
>
const pullOrFlush: {
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;
}
pullOrFlush
})
)