<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>>Applies a function to the element at the specified index safely, returning the
updated array in Option.some.
When to use
Use to derive a replacement value from an array element at a specific index while leaving the other elements unchanged.
Details
Returns Option.none() when the index is out of bounds.
Example (Modifying an element)
import { Array } from "effect"
console.log(Array.modify([1, 2, 3, 4], 2, (n) => n * 2)) // Option.some([1, 2, 6, 4])
console.log(Array.modify([1, 2, 3, 4], 5, (n) => n * 2)) // Option.none()export const 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
>
>
}
Applies a function to the element at the specified index safely, returning the
updated array in Option.some.
When to use
Use to derive a replacement value from an array element at a specific index
while leaving the other elements unchanged.
Details
Returns Option.none() when the index is out of bounds.
Example (Modifying an element)
import { Array } from "effect"
console.log(Array.modify([1, 2, 3, 4], 2, (n) => n * 2)) // Option.some([1, 2, 6, 4])
console.log(Array.modify([1, 2, 3, 4], 5, (n) => n * 2)) // Option.none()
modify: {
<function (type parameter) A in <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, function (type parameter) B in <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>>B, function (type parameter) S in <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>>S extends interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <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> = interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <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>>(
i: numberi: number,
f: (a: ReadonlyArray.Infer<S>) => Bf: (a: ReadonlyArray.Infer<S>a: 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>>(i: number, f: (a: ReadonlyArray.Infer<S>) => B): (self: S) => Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S>) => function (type parameter) B in <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>>B
): (self: S extends Iterable<A> = Iterable<A>self: function (type parameter) S in <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>>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, 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>>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>>(i: number, f: (a: ReadonlyArray.Infer<S>) => B): (self: S) => Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S> | function (type parameter) B in <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>>B>>
<function (type parameter) A in <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>>A, function (type parameter) B in <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>>B, function (type parameter) S in <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>>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, f: (a: ReadonlyArray.Infer<S>) => 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, f: (a: ReadonlyArray.Infer<S>) => 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, f: (a: ReadonlyArray.Infer<S>) => B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>S,
i: numberi: number,
f: (a: ReadonlyArray.Infer<S>) => Bf: (a: ReadonlyArray.Infer<S>a: 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, f: (a: ReadonlyArray.Infer<S>) => 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, f: (a: ReadonlyArray.Infer<S>) => 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, f: (a: ReadonlyArray.Infer<S>) => 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, f: (a: ReadonlyArray.Infer<S>) => 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, f: (a: ReadonlyArray.Infer<S>) => B): Option.Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>B>>
} = dual<(...args: Array<any>) => any, <A, B>(self: Iterable<A>, i: number, f: (a: A) => B) => Option.Option<Array<A | B>>>(arity: 3, body: <A, B>(self: Iterable<A>, i: number, f: (a: A) => B) => Option.Option<Array<A | B>>): ((...args: Array<any>) => any) & (<A, B>(self: Iterable<A>, i: number, f: (a: A) => 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, f: (a: A) => B): Option.Option<Array<A | B>>A, function (type parameter) B in <A, B>(self: Iterable<A>, i: number, f: (a: A) => 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, f: (a: A) => B): Option.Option<Array<A | B>>A>, i: numberi: number, f: (a: A) => Bf: (a: Aa: function (type parameter) A in <A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Option.Option<Array<A | B>>A) => function (type parameter) B in <A, B>(self: Iterable<A>, i: number, f: (a: A) => 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, f: (a: A) => B): Option.Option<Array<A | B>>A | function (type parameter) B in <A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Option.Option<Array<A | B>>B>> => {
const const arr: A[]arr = const Array: ArrayConstructorExposes the global array constructor.
When to use
Use to access native JavaScript array constructor methods such as isArray
or from from the Effect module namespace.
Example (Accessing the Array constructor)
import { Array } from "effect"
const arr = new Array.Array(3)
console.log(arr) // [undefined, undefined, undefined]
Array.ArrayConstructor.from<A>(iterable: Iterable<A> | ArrayLike<A>): A[] (+3 overloads)Creates an array from an iterable object.
from(self: Iterable<A>self)
if (function isOutOfBounds<A>(
i: number,
as: readonly A[]
): boolean
isOutOfBounds(i: numberi, const arr: A[]arr)) {
return 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()
}
const const out: (A | B)[]out: interface Array<T>Array<function (type parameter) A in <A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Option.Option<Array<A | B>>A | function (type parameter) B in <A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Option.Option<Array<A | B>>B> = const arr: A[]arr
const const b: Bb = f: (a: A) => Bf(const arr: A[]arr[i: numberi])
const out: (A | B)[]out[i: numberi] = const b: Bb
return 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(const out: (A | B)[]out)
})