<A, B>(self: Chunk<Result<B, A>>): [Chunk<A>, Chunk<B>]Separates a chunk of Result values into a chunk of failures and a chunk of
successes.
Details
The returned tuple is [failures, successes], preserving the original order
within each side.
Example (Separating failures and successes)
import { Chunk, Result } from "effect"
const chunk = Chunk.make(
Result.succeed(1),
Result.fail("error1"),
Result.succeed(2),
Result.fail("error2"),
Result.succeed(3)
)
const [errors, values] = Chunk.separate(chunk)
console.log(Chunk.toArray(errors)) // ["error1", "error2"]
console.log(Chunk.toArray(values)) // [1, 2, 3]
// All successes
const allSuccesses = Chunk.make(Result.succeed(1), Result.succeed(2))
const [noErrors, allValues] = Chunk.separate(allSuccesses)
console.log(Chunk.toArray(noErrors)) // []
console.log(Chunk.toArray(allValues)) // [1, 2]export const const separate: <A, B>(
self: Chunk<Result<B, A>>
) => [Chunk<A>, Chunk<B>]
Separates a chunk of Result values into a chunk of failures and a chunk of
successes.
Details
The returned tuple is [failures, successes], preserving the original order
within each side.
Example (Separating failures and successes)
import { Chunk, Result } from "effect"
const chunk = Chunk.make(
Result.succeed(1),
Result.fail("error1"),
Result.succeed(2),
Result.fail("error2"),
Result.succeed(3)
)
const [errors, values] = Chunk.separate(chunk)
console.log(Chunk.toArray(errors)) // ["error1", "error2"]
console.log(Chunk.toArray(values)) // [1, 2, 3]
// All successes
const allSuccesses = Chunk.make(Result.succeed(1), Result.succeed(2))
const [noErrors, allValues] = Chunk.separate(allSuccesses)
console.log(Chunk.toArray(noErrors)) // []
console.log(Chunk.toArray(allValues)) // [1, 2]
separate = <function (type parameter) A in <A, B>(self: Chunk<Result<B, A>>): [Chunk<A>, Chunk<B>]A, function (type parameter) B in <A, B>(self: Chunk<Result<B, A>>): [Chunk<A>, Chunk<B>]B>(self: Chunk<Result<B, A>>(parameter) self: {
length: number;
right: Chunk<A>;
left: Chunk<A>;
backing: Backing<A>;
depth: number;
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;
}
self: interface Chunk<out A>A Chunk is an immutable, ordered collection optimized for efficient concatenation and access patterns.
Example (Inspecting chunk values)
import { Chunk } from "effect"
const chunk: Chunk.Chunk<number> = Chunk.make(1, 2, 3)
console.log(chunk.length) // 3
console.log(Chunk.toArray(chunk)) // [1, 2, 3]
A namespace containing utility types for Chunk operations.
Example (Working with Chunk utility types)
import type { Chunk } from "effect"
// Extract the element type from a Chunk
declare const chunk: Chunk.Chunk<string>
type ElementType = Chunk.Chunk.Infer<typeof chunk> // string
// Create a preserving non-emptiness
declare const nonEmptyChunk: Chunk.NonEmptyChunk<number>
type WithString = Chunk.Chunk.With<typeof nonEmptyChunk, string> // Chunk.NonEmptyChunk<string>
Chunk<type Result<A, E = never> = R.Success<A, E> | R.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) B in <A, B>(self: Chunk<Result<B, A>>): [Chunk<A>, Chunk<B>]B, function (type parameter) A in <A, B>(self: Chunk<Result<B, A>>): [Chunk<A>, Chunk<B>]A>>): [interface Chunk<out A>A Chunk is an immutable, ordered collection optimized for efficient concatenation and access patterns.
Example (Inspecting chunk values)
import { Chunk } from "effect"
const chunk: Chunk.Chunk<number> = Chunk.make(1, 2, 3)
console.log(chunk.length) // 3
console.log(Chunk.toArray(chunk)) // [1, 2, 3]
A namespace containing utility types for Chunk operations.
Example (Working with Chunk utility types)
import type { Chunk } from "effect"
// Extract the element type from a Chunk
declare const chunk: Chunk.Chunk<string>
type ElementType = Chunk.Chunk.Infer<typeof chunk> // string
// Create a preserving non-emptiness
declare const nonEmptyChunk: Chunk.NonEmptyChunk<number>
type WithString = Chunk.Chunk.With<typeof nonEmptyChunk, string> // Chunk.NonEmptyChunk<string>
Chunk<function (type parameter) A in <A, B>(self: Chunk<Result<B, A>>): [Chunk<A>, Chunk<B>]A>, interface Chunk<out A>A Chunk is an immutable, ordered collection optimized for efficient concatenation and access patterns.
Example (Inspecting chunk values)
import { Chunk } from "effect"
const chunk: Chunk.Chunk<number> = Chunk.make(1, 2, 3)
console.log(chunk.length) // 3
console.log(Chunk.toArray(chunk)) // [1, 2, 3]
A namespace containing utility types for Chunk operations.
Example (Working with Chunk utility types)
import type { Chunk } from "effect"
// Extract the element type from a Chunk
declare const chunk: Chunk.Chunk<string>
type ElementType = Chunk.Chunk.Infer<typeof chunk> // string
// Create a preserving non-emptiness
declare const nonEmptyChunk: Chunk.NonEmptyChunk<number>
type WithString = Chunk.Chunk.With<typeof nonEmptyChunk, string> // Chunk.NonEmptyChunk<string>
Chunk<function (type parameter) B in <A, B>(self: Chunk<Result<B, A>>): [Chunk<A>, Chunk<B>]B>] =>
pipe<any, [Chunk<A>, Chunk<B>]>(a: any, ab: (a: any) => [Chunk<A>, Chunk<B>]): [Chunk<A>, Chunk<B>] (+19 overloads)Pipes the value of an expression through a left-to-right sequence of
functions.
When to use
Use when you need to compose data-last functions into readable
transformation pipelines instead of method-style chains.
Details
Takes an initial value, passes it to the first function, then passes each
result to the next function in order. The final function result is returned.
Gotchas
Each function passed after the initial value must accept a single argument,
because pipe calls each step with only the previous result.
Example (Piping values through functions)
In this example, 1 is passed to the first function, and each result becomes
the input for the next function.
import { pipe } from "effect"
const result = pipe(
1,
(n) => n + 1,
(n) => n * 2,
(n) => `result: ${n}`
)
console.log(result) // "result: 4"
Example (Chaining methods before conversion)
const numbers = [1, 2, 3, 4]
const double = (n: number) => n * 2
const greaterThanFour = (n: number) => n > 4
const result = numbers.map(double).filter(greaterThanFour)
console.log(result) // [6, 8]
Example (Rewriting method chains with pipe)
The same transformation can be written with data-last functions.
import { Array, pipe } from "effect"
const numbers = [1, 2, 3, 4]
const double = (n: number) => n * 2
const greaterThanFour = (n: number) => n > 4
const result = pipe(
numbers,
Array.map(double),
Array.filter(greaterThanFour)
)
console.log(result) // [6, 8]
Example (Chaining arithmetic operations)
import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2
Example (Building a simple transformation pipeline)
import { pipe } from "effect"
// Simple transformation pipeline
const result = pipe(
5,
(x) => x * 2, // 10
(x) => x + 1, // 11
(x) => x.toString() // "11"
)
console.log(result) // "11"
pipe(
import RARA.separate(const toReadonlyArray: <
S extends Chunk<any>
>(
self: S
) => S extends NonEmptyChunk<any>
? RA.NonEmptyReadonlyArray<Chunk.Infer<S>>
: ReadonlyArray<Chunk.Infer<S>>
Converts a Chunk into a ReadonlyArray. If the provided Chunk is
non-empty (NonEmptyChunk), the function will return a
NonEmptyReadonlyArray, ensuring the non-empty property is preserved.
Example (Converting chunks to readonly arrays)
import { Chunk } from "effect"
const chunk = Chunk.make(1, 2, 3)
const readonlyArray = Chunk.toReadonlyArray(chunk)
console.log(readonlyArray) // [1, 2, 3]
// The result is read-only, modifications would cause TypeScript errors
// readonlyArray[0] = 10 // TypeScript error
// With empty chunk
const emptyChunk = Chunk.empty<number>()
console.log(Chunk.toReadonlyArray(emptyChunk)) // []
toReadonlyArray(self: Chunk<Result<B, A>>(parameter) self: {
length: number;
right: Chunk<A>;
left: Chunk<A>;
backing: Backing<A>;
depth: number;
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;
}
self)),
([l: anyl, r: anyr]) => [const fromArrayUnsafe: <A>(
self: ReadonlyArray<A>
) => Chunk<A>
Wraps an array into a chunk without copying.
When to use
Use when the input array can be shared with the resulting Chunk and avoiding
a copy matters.
Gotchas
Mutating the source array after wrapping can mutate the resulting Chunk.
Example (Creating chunks without copying arrays)
import { Chunk } from "effect"
const array = [1, 2, 3, 4, 5]
const chunk = Chunk.fromArrayUnsafe(array)
console.log(Chunk.toArray(chunk)) // [1, 2, 3, 4, 5]
// Warning: Since this doesn't copy the array, mutations affect the chunk
array[0] = 999
console.log(Chunk.toArray(chunk)) // [999, 2, 3, 4, 5]
fromArrayUnsafe(l: anyl), const fromArrayUnsafe: <A>(
self: ReadonlyArray<A>
) => Chunk<A>
Wraps an array into a chunk without copying.
When to use
Use when the input array can be shared with the resulting Chunk and avoiding
a copy matters.
Gotchas
Mutating the source array after wrapping can mutate the resulting Chunk.
Example (Creating chunks without copying arrays)
import { Chunk } from "effect"
const array = [1, 2, 3, 4, 5]
const chunk = Chunk.fromArrayUnsafe(array)
console.log(Chunk.toArray(chunk)) // [1, 2, 3, 4, 5]
// Warning: Since this doesn't copy the array, mutations affect the chunk
array[0] = 999
console.log(Chunk.toArray(chunk)) // [999, 2, 3, 4, 5]
fromArrayUnsafe(r: anyr)]
)