<A>(f: (i: number) => A): (n: number) => NonEmptyArray<A>
<A>(n: number, f: (i: number) => A): NonEmptyArray<A>Creates a NonEmptyArray of length n where element i is computed by f(i).
When to use
Use when you need to compute each array element from its index.
Details
n is normalized to an integer greater than or equal to 1, so this function
always returns at least one element. Supports both data-first and data-last
usage.
Example (Generating values from indices)
import { Array } from "effect"
const result = Array.makeBy(5, (n) => n * 2)
console.log(result) // [0, 2, 4, 6, 8]export const const makeBy: {
<A>(f: (i: number) => A): (
n: number
) => NonEmptyArray<A>
<A>(
n: number,
f: (i: number) => A
): NonEmptyArray<A>
}
Creates a NonEmptyArray of length n where element i is computed by f(i).
When to use
Use when you need to compute each array element from its index.
Details
n is normalized to an integer greater than or equal to 1, so this function
always returns at least one element. Supports both data-first and data-last
usage.
Example (Generating values from indices)
import { Array } from "effect"
const result = Array.makeBy(5, (n) => n * 2)
console.log(result) // [0, 2, 4, 6, 8]
makeBy: {
<function (type parameter) A in <A>(f: (i: number) => A): (n: number) => NonEmptyArray<A>A>(f: (i: number) => Af: (i: numberi: number) => function (type parameter) A in <A>(f: (i: number) => A): (n: number) => NonEmptyArray<A>A): (n: numbern: number) => type NonEmptyArray<A> = [A, ...A[]]A mutable array guaranteed to have at least one element.
When to use
Use when mutation is acceptable and non-emptiness must be tracked at the type
level.
Details
This is the mutable counterpart of
NonEmptyReadonlyArray
. Most Array
module functions return NonEmptyArray when the result is guaranteed
non-empty.
Example (Typing a mutable non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyArray<number> = [1, 2, 3]
nonEmpty.push(4)
NonEmptyArray<function (type parameter) A in <A>(f: (i: number) => A): (n: number) => NonEmptyArray<A>A>
<function (type parameter) A in <A>(n: number, f: (i: number) => A): NonEmptyArray<A>A>(n: numbern: number, f: (i: number) => Af: (i: numberi: number) => function (type parameter) A in <A>(n: number, f: (i: number) => A): NonEmptyArray<A>A): type NonEmptyArray<A> = [A, ...A[]]A mutable array guaranteed to have at least one element.
When to use
Use when mutation is acceptable and non-emptiness must be tracked at the type
level.
Details
This is the mutable counterpart of
NonEmptyReadonlyArray
. Most Array
module functions return NonEmptyArray when the result is guaranteed
non-empty.
Example (Typing a mutable non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyArray<number> = [1, 2, 3]
nonEmpty.push(4)
NonEmptyArray<function (type parameter) A in <A>(n: number, f: (i: number) => A): NonEmptyArray<A>A>
} = dual<(...args: Array<any>) => any, <A>(n: number, f: (i: number) => A) => NonEmptyArray<A>>(arity: 2, body: <A>(n: number, f: (i: number) => A) => NonEmptyArray<A>): ((...args: Array<any>) => any) & (<A>(n: number, f: (i: number) => A) => NonEmptyArray<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>(n: number, f: (i: number) => A): NonEmptyArray<A>A>(n: numbern: number, f: (i: number) => Af: (i: numberi: number) => function (type parameter) A in <A>(n: number, f: (i: number) => A): NonEmptyArray<A>A) => {
const const max: numbermax = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.max(...values: number[]): numberReturns the larger of a set of supplied numeric expressions.
max(1, 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(n: numbern))
const const out: any[]out = new const Array: ArrayConstructor
new (arrayLength?: number) => any[] (+2 overloads)
Array(const max: numbermax)
for (let let i: numberi = 0; let i: numberi < const max: numbermax; let i: numberi++) {
const out: any[]out[let i: numberi] = f: (i: number) => Af(let i: numberi)
}
return const out: any[]out as type NonEmptyArray<A> = [A, ...A[]]A mutable array guaranteed to have at least one element.
When to use
Use when mutation is acceptable and non-emptiness must be tracked at the type
level.
Details
This is the mutable counterpart of
NonEmptyReadonlyArray
. Most Array
module functions return NonEmptyArray when the result is guaranteed
non-empty.
Example (Typing a mutable non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyArray<number> = [1, 2, 3]
nonEmpty.push(4)
NonEmptyArray<function (type parameter) A in <A>(n: number, f: (i: number) => A): NonEmptyArray<A>A>
})