<A, B, X>(f: Filter.Filter<A, B, X>): (self: Chunk<A>) => Chunk<B>
<A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>Transforms all elements of the chunk for as long as the specified function succeeds.
Example (Filtering and mapping while values match)
import { Chunk, Result } from "effect"
const chunk = Chunk.make("1", "2", "hello", "3", "4")
const result = Chunk.filterMapWhile(chunk, (s) => {
const num = parseInt(s)
return isNaN(num) ? Result.failVoid : Result.succeed(num)
})
console.log(Chunk.toArray(result)) // [1, 2]
// Stops at "hello" and doesn't process "3", "4"
// Compare with regular filterMap
const allNumbers = Chunk.filterMap(chunk, (s) => {
const num = parseInt(s)
return isNaN(num) ? Result.failVoid : Result.succeed(num)
})
console.log(Chunk.toArray(allNumbers)) // [1, 2, 3, 4]export const const filterMapWhile: {
<A, B, X>(f: Filter.Filter<A, B, X>): (
self: Chunk<A>
) => Chunk<B>
<A, B, X>(
self: Chunk<A>,
f: Filter.Filter<A, B, X>
): Chunk<B>
}
Transforms all elements of the chunk for as long as the specified function succeeds.
Example (Filtering and mapping while values match)
import { Chunk, Result } from "effect"
const chunk = Chunk.make("1", "2", "hello", "3", "4")
const result = Chunk.filterMapWhile(chunk, (s) => {
const num = parseInt(s)
return isNaN(num) ? Result.failVoid : Result.succeed(num)
})
console.log(Chunk.toArray(result)) // [1, 2]
// Stops at "hello" and doesn't process "3", "4"
// Compare with regular filterMap
const allNumbers = Chunk.filterMap(chunk, (s) => {
const num = parseInt(s)
return isNaN(num) ? Result.failVoid : Result.succeed(num)
})
console.log(Chunk.toArray(allNumbers)) // [1, 2, 3, 4]
filterMapWhile: {
<function (type parameter) A in <A, B, X>(f: Filter.Filter<A, B, X>): (self: Chunk<A>) => Chunk<B>A, function (type parameter) B in <A, B, X>(f: Filter.Filter<A, B, X>): (self: Chunk<A>) => Chunk<B>B, function (type parameter) X in <A, B, X>(f: Filter.Filter<A, B, X>): (self: Chunk<A>) => Chunk<B>X>(f: Filter.Filter<A, B, X>f: import FilterFilter.interface Filter<in Input, out Pass = Input, out Fail = Input>Represents a filter function that can transform inputs to outputs or filter them out.
Details
A filter takes an input value and either returns a boxed pass value or the
special fail type to indicate the value should be filtered out.
Example (Defining a positive number filter)
import { Filter, Result } from "effect"
// A filter that only passes positive numbers
const positiveFilter: Filter.Filter<number> = (n) => n > 0 ? Result.succeed(n) : Result.fail(n)
console.log(positiveFilter(5)) // Result.succeed(5)
console.log(positiveFilter(-3)) // Result.fail(-3)
Filter<function (type parameter) A in <A, B, X>(f: Filter.Filter<A, B, X>): (self: Chunk<A>) => Chunk<B>A, function (type parameter) B in <A, B, X>(f: Filter.Filter<A, B, X>): (self: Chunk<A>) => Chunk<B>B, function (type parameter) X in <A, B, X>(f: Filter.Filter<A, B, X>): (self: Chunk<A>) => Chunk<B>X>): (self: Chunk<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<function (type parameter) A in <A, B, X>(f: Filter.Filter<A, B, X>): (self: 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, X>(f: Filter.Filter<A, B, X>): (self: Chunk<A>) => Chunk<B>B>
<function (type parameter) A in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>A, function (type parameter) B in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>B, function (type parameter) X in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>X>(self: Chunk<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<function (type parameter) A in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>A>, f: Filter.Filter<A, B, X>f: import FilterFilter.interface Filter<in Input, out Pass = Input, out Fail = Input>Represents a filter function that can transform inputs to outputs or filter them out.
Details
A filter takes an input value and either returns a boxed pass value or the
special fail type to indicate the value should be filtered out.
Example (Defining a positive number filter)
import { Filter, Result } from "effect"
// A filter that only passes positive numbers
const positiveFilter: Filter.Filter<number> = (n) => n > 0 ? Result.succeed(n) : Result.fail(n)
console.log(positiveFilter(5)) // Result.succeed(5)
console.log(positiveFilter(-3)) // Result.fail(-3)
Filter<function (type parameter) A in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>A, function (type parameter) B in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>B, function (type parameter) X in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>X>): 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, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>B>
} = dual<(...args: Array<any>) => any, <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>) => Chunk<B>>(arity: 2, body: <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>) => Chunk<B>): ((...args: Array<any>) => any) & (<A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>) => Chunk<B>) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(2, <function (type parameter) A in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>A, function (type parameter) B in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>B, function (type parameter) X in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>X>(self: Chunk<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<function (type parameter) A in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>A>, f: Filter.Filter<A, B, X>f: import FilterFilter.interface Filter<in Input, out Pass = Input, out Fail = Input>Represents a filter function that can transform inputs to outputs or filter them out.
Details
A filter takes an input value and either returns a boxed pass value or the
special fail type to indicate the value should be filtered out.
Example (Defining a positive number filter)
import { Filter, Result } from "effect"
// A filter that only passes positive numbers
const positiveFilter: Filter.Filter<number> = (n) => n > 0 ? Result.succeed(n) : Result.fail(n)
console.log(positiveFilter(5)) // Result.succeed(5)
console.log(positiveFilter(-3)) // Result.fail(-3)
Filter<function (type parameter) A in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>A, function (type parameter) B in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>B, function (type parameter) X in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>X>): 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, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>B> => {
const const out: B[]out: interface Array<T>Array<function (type parameter) B in <A, B, X>(self: Chunk<A>, f: Filter.Filter<A, B, X>): Chunk<B>B> = []
for (const const a: Aa of self: Chunk<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) {
const const result: R.Result<B, X>result = f: Filter.Filter<A, B, X>f(const a: Aa)
if (import RR.const isSuccess: <A, E>(
self: Result<A, E>
) => self is Success<A, E>
Checks whether a Result is a Success.
When to use
Use to narrow a known Result to the Success variant.
Details
- Acts as a TypeScript type guard, narrowing to
Success<A, E>
- After narrowing, you can access
.success to read the value
Example (Narrowing to success)
import { Result } from "effect"
const result = Result.succeed(42)
if (Result.isSuccess(result)) {
console.log(result.success)
// Output: 42
}
isSuccess(const result: R.Result<B, X>result)) {
const out: B[]out.Array<B>.push(...items: B[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const result: R.Success<B, X>const result: {
_tag: "Success";
_op: "Success";
success: 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;
}
result.Success<B, X>.success: Bsuccess)
} else {
break
}
}
return 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(const out: B[]out)
})