(index: number): <A>(self: ReadonlyArray<A>) => Option.Option<A>
<A>(self: ReadonlyArray<A>, index: number): Option.Option<A>Reads an element at the given index safely, returning Option.some or
Option.none if the index is out of bounds.
When to use
Use when you need to read an array element by index and handle an
out-of-bounds index as Option.none.
Details
The index is floored to an integer. This never throws.
Example (Accessing indexes safely)
import { Array } from "effect"
console.log(Array.get([1, 2, 3], 1)) // Some(2)
console.log(Array.get([1, 2, 3], 10)) // Noneexport const const get: {
(index: number): <A>(
self: ReadonlyArray<A>
) => Option.Option<A>
<A>(
self: ReadonlyArray<A>,
index: number
): Option.Option<A>
}
Reads an element at the given index safely, returning Option.some or
Option.none if the index is out of bounds.
When to use
Use when you need to read an array element by index and handle an
out-of-bounds index as Option.none.
Details
The index is floored to an integer. This never throws.
Example (Accessing indexes safely)
import { Array } from "effect"
console.log(Array.get([1, 2, 3], 1)) // Some(2)
console.log(Array.get([1, 2, 3], 10)) // None
get: {
(index: numberindex: number): <function (type parameter) A in <A>(self: ReadonlyArray<A>): Option.Option<A>A>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A>(self: ReadonlyArray<A>): Option.Option<A>A>) => 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<function (type parameter) A in <A>(self: ReadonlyArray<A>): Option.Option<A>A>
<function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): Option.Option<A>A>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): Option.Option<A>A>, index: numberindex: number): 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<function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): Option.Option<A>A>
} = dual<(...args: Array<any>) => any, <A>(self: ReadonlyArray<A>, index: number) => Option.Option<A>>(arity: 2, body: <A>(self: ReadonlyArray<A>, index: number) => Option.Option<A>): ((...args: Array<any>) => any) & (<A>(self: ReadonlyArray<A>, index: number) => Option.Option<A>) (+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>(self: ReadonlyArray<A>, index: number): Option.Option<A>A>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): Option.Option<A>A>, index: numberindex: number): 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<function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): Option.Option<A>A> => {
const const i: numberi = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.floor(x: number): numberReturns the greatest integer less than or equal to its numeric argument.
floor(index: numberindex)
return function isOutOfBounds<A>(
i: number,
as: readonly A[]
): boolean
isOutOfBounds(const i: numberi, self: readonly A[]self) ? 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() : 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(self: readonly A[]self[const i: numberi])
})