<B>(i: number, b: B): <A, S extends Iterable<A> = Iterable<A>>(
self: S
) => Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
b: B
): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>Replaces the element at the specified index safely with a new value, returning the
updated array in Option.some.
When to use
Use to set a fixed replacement value at a specific index.
Details
Returns Option.none() when the index is out of bounds.
Example (Replacing an element)
import { Array } from "effect"
console.log(Array.replace([1, 2, 3], 1, 4)) // Option.some([1, 4, 3])export const const replace: {
<B>(i: number, b: B): <
A,
S extends Iterable<A> = Iterable<A>
>(
self: S
) => Option.Option<
ReadonlyArray.With<
S,
ReadonlyArray.Infer<S> | B
>
>
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
b: B
): Option.Option<
ReadonlyArray.With<
S,
ReadonlyArray.Infer<S> | B
>
>
}
Replaces the element at the specified index safely with a new value, returning the
updated array in Option.some.
When to use
Use to set a fixed replacement value at a specific index.
Details
Returns Option.none() when the index is out of bounds.
Example (Replacing an element)
import { Array } from "effect"
console.log(Array.replace([1, 2, 3], 1, 4)) // Option.some([1, 4, 3])
replace: {
<function (type parameter) B in <B>(i: number, b: B): <A, S extends Iterable<A> = Iterable<A>>(self: S) => Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>B>(i: numberi: number, b: Bb: function (type parameter) B in <B>(i: number, b: B): <A, S extends Iterable<A> = Iterable<A>>(self: S) => Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>B): <function (type parameter) A in <A, S extends Iterable<A> = Iterable<A>>(self: S): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>A, function (type parameter) S in <A, S extends Iterable<A> = Iterable<A>>(self: S): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S extends interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, S extends Iterable<A> = Iterable<A>>(self: S): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>A> = interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, S extends Iterable<A> = Iterable<A>>(self: S): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>A>>(
self: S extends Iterable<A> = Iterable<A>self: function (type parameter) S in <A, S extends Iterable<A> = Iterable<A>>(self: S): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S
) => 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<ReadonlyArray.type ReadonlyArray.With<S extends Iterable<any>, A> = S extends readonly [any, ...any[]] ? [A, ...A[]] : A[]Constructs an array type preserving non-emptiness.
Example (Preserving non-emptiness)
import type { Array } from "effect"
type Result = Array.ReadonlyArray.With<readonly [number], string>
// Result is NonEmptyArray<string>
With<function (type parameter) S in <A, S extends Iterable<A> = Iterable<A>>(self: S): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S, ReadonlyArray.type ReadonlyArray.Infer<S extends Iterable<any>> = S extends readonly (infer A)[] ? A : S extends Iterable<infer A> ? A : neverInfers the element type of an iterable.
Example (Inferring an element type)
import type { Array } from "effect"
type StringArrayType = Array.ReadonlyArray.Infer<ReadonlyArray<string>>
// StringArrayType is string
Infer<function (type parameter) S in <A, S extends Iterable<A> = Iterable<A>>(self: S): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S> | function (type parameter) B in <B>(i: number, b: B): <A, S extends Iterable<A> = Iterable<A>>(self: S) => Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>B>>
<function (type parameter) A in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>A, function (type parameter) B in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>B, function (type parameter) S in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S extends interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>A> = interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>A>>(
self: S extends Iterable<A> = Iterable<A>self: function (type parameter) S in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S,
i: numberi: number,
b: Bb: function (type parameter) B in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>B
): 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<ReadonlyArray.type ReadonlyArray.With<S extends Iterable<any>, A> = S extends readonly [any, ...any[]] ? [A, ...A[]] : A[]Constructs an array type preserving non-emptiness.
Example (Preserving non-emptiness)
import type { Array } from "effect"
type Result = Array.ReadonlyArray.With<readonly [number], string>
// Result is NonEmptyArray<string>
With<function (type parameter) S in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S, ReadonlyArray.type ReadonlyArray.Infer<S extends Iterable<any>> = S extends readonly (infer A)[] ? A : S extends Iterable<infer A> ? A : neverInfers the element type of an iterable.
Example (Inferring an element type)
import type { Array } from "effect"
type StringArrayType = Array.ReadonlyArray.Infer<ReadonlyArray<string>>
// StringArrayType is string
Infer<function (type parameter) S in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S> | function (type parameter) B in <A, B, S extends Iterable<A> = Iterable<A>>(self: S, i: number, b: B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>B>>
} = dual<(...args: Array<any>) => any, <A, B>(self: Iterable<A>, i: number, b: B) => Option.Option<Array<A | B>>>(arity: 3, body: <A, B>(self: Iterable<A>, i: number, b: B) => Option.Option<Array<A | B>>): ((...args: Array<any>) => any) & (<A, B>(self: Iterable<A>, i: number, b: B) => Option.Option<Array<A | 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(
3,
<function (type parameter) A in <A, B>(self: Iterable<A>, i: number, b: B): Option.Option<Array<A | B>>A, function (type parameter) B in <A, B>(self: Iterable<A>, i: number, b: B): Option.Option<Array<A | B>>B>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, i: number, b: B): Option.Option<Array<A | B>>A>, i: numberi: number, b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, i: number, b: B): Option.Option<Array<A | B>>B): 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<interface Array<T>Array<function (type parameter) A in <A, B>(self: Iterable<A>, i: number, b: B): Option.Option<Array<A | B>>A | function (type parameter) B in <A, B>(self: Iterable<A>, i: number, b: B): Option.Option<Array<A | B>>B>> => const modify: {
<A, B, S extends Iterable<A> = Iterable<A>>(
i: number,
f: (a: ReadonlyArray.Infer<S>) => B
): (
self: S
) => Option.Option<
ReadonlyArray.With<
S,
ReadonlyArray.Infer<S> | B
>
>
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
f: (a: ReadonlyArray.Infer<S>) => B
): Option.Option<
ReadonlyArray.With<
S,
ReadonlyArray.Infer<S> | B
>
>
}
modify(self: Iterable<A>self, i: numberi, () => b: Bb)
)