(index: number): <A>(self: ReadonlyArray<A>) => A
<A>(self: ReadonlyArray<A>, index: number): AReads an element at the given index, throwing if the index is out of bounds.
When to use
Use to read an array element at a known valid index when out-of-bounds would be a programming error.
Details
Throws an Error with the message "Index out of bounds: <i>". Prefer
get for safe access.
Example (Accessing indexes unsafely)
import { Array } from "effect"
console.log(Array.getUnsafe([1, 2, 3], 1)) // 2
// Array.getUnsafe([1, 2, 3], 10) // throws Errorexport const const getUnsafe: {
(index: number): <A>(
self: ReadonlyArray<A>
) => A
<A>(self: ReadonlyArray<A>, index: number): A
}
Reads an element at the given index, throwing if the index is out of bounds.
When to use
Use to read an array element at a known valid index when out-of-bounds would
be a programming error.
Details
Throws an Error with the message "Index out of bounds: <i>". Prefer
get for safe access.
Example (Accessing indexes unsafely)
import { Array } from "effect"
console.log(Array.getUnsafe([1, 2, 3], 1)) // 2
// Array.getUnsafe([1, 2, 3], 10) // throws Error
getUnsafe: {
(index: numberindex: number): <function (type parameter) A in <A>(self: ReadonlyArray<A>): AA>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A>(self: ReadonlyArray<A>): AA>) => function (type parameter) A in <A>(self: ReadonlyArray<A>): AA
<function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): AA>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): AA>, index: numberindex: number): function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): AA
} = dual<(...args: Array<any>) => any, <A>(self: ReadonlyArray<A>, index: number) => A>(arity: 2, body: <A>(self: ReadonlyArray<A>, index: number) => A): ((...args: Array<any>) => any) & (<A>(self: ReadonlyArray<A>, index: number) => 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): AA>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): AA>, index: numberindex: number): function (type parameter) A in <A>(self: ReadonlyArray<A>, index: number): AA => {
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)
if (function isOutOfBounds<A>(
i: number,
as: readonly A[]
): boolean
isOutOfBounds(const i: numberi, self: readonly A[]self)) {
throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error(`Index out of bounds: ${const i: numberi}`)
}
return self: readonly A[]self[const i: numberi]
})