<A, T>(n: number, fill: T): (self: Array<A>) => Array<A | T>
<A, T>(self: Array<A>, n: number, fill: T): Array<A | T>Pads or truncates an array to exactly n elements, filling with fill
if the array is shorter, or slicing if longer.
When to use
Use to ensure an array has a specific length, padding with a fill value or truncating as needed.
Details
Returns an empty array when n <= 0.
Example (Padding an array)
import { Array } from "effect"
console.log(Array.pad([1, 2, 3], 6, 0)) // [1, 2, 3, 0, 0, 0]export const const pad: {
<A, T>(n: number, fill: T): (
self: Array<A>
) => Array<A | T>
<A, T>(
self: Array<A>,
n: number,
fill: T
): Array<A | T>
}
Pads or truncates an array to exactly n elements, filling with fill
if the array is shorter, or slicing if longer.
When to use
Use to ensure an array has a specific length, padding with a fill value or truncating as needed.
Details
Returns an empty array when n <= 0.
Example (Padding an array)
import { Array } from "effect"
console.log(Array.pad([1, 2, 3], 6, 0)) // [1, 2, 3, 0, 0, 0]
pad: {
<function (type parameter) A in <A, T>(n: number, fill: T): (self: Array<A>) => Array<A | T>A, function (type parameter) T in <A, T>(n: number, fill: T): (self: Array<A>) => Array<A | T>T>(
n: numbern: number,
fill: Tfill: function (type parameter) T in <A, T>(n: number, fill: T): (self: Array<A>) => Array<A | T>T
): (
self: A[]self: interface Array<T>Array<function (type parameter) A in <A, T>(n: number, fill: T): (self: Array<A>) => Array<A | T>A>
) => interface Array<T>Array<function (type parameter) A in <A, T>(n: number, fill: T): (self: Array<A>) => Array<A | T>A | function (type parameter) T in <A, T>(n: number, fill: T): (self: Array<A>) => Array<A | T>T>
<function (type parameter) A in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>A, function (type parameter) T in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>T>(self: A[]self: interface Array<T>Array<function (type parameter) A in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>A>, n: numbern: number, fill: Tfill: function (type parameter) T in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>T): interface Array<T>Array<function (type parameter) A in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>A | function (type parameter) T in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>T>
} = dual<(...args: Array<any>) => any, <A, T>(self: Array<A>, n: number, fill: T) => Array<A | T>>(arity: 3, body: <A, T>(self: Array<A>, n: number, fill: T) => Array<A | T>): ((...args: Array<any>) => any) & (<A, T>(self: Array<A>, n: number, fill: T) => Array<A | T>) (+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, T>(self: Array<A>, n: number, fill: T): Array<A | T>A, function (type parameter) T in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>T>(self: A[]self: interface Array<T>Array<function (type parameter) A in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>A>, n: numbern: number, fill: Tfill: function (type parameter) T in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>T): interface Array<T>Array<function (type parameter) A in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>A | function (type parameter) T in <A, T>(self: Array<A>, n: number, fill: T): Array<A | T>T> => {
if (self: A[]self.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length >= n: numbern) {
return const take: <A>(self: Iterable<A>, n: number) => A[] (+1 overload)take(self: A[]self, n: numbern)
}
return const appendAll: {
<
S extends Iterable<any>,
T extends Iterable<any>
>(
that: T
): (
self: S
) => ReadonlyArray.OrNonEmpty<
S,
T,
| ReadonlyArray.Infer<S>
| ReadonlyArray.Infer<T>
>
<A, B>(
self: Iterable<A>,
that: NonEmptyReadonlyArray<B>
): NonEmptyArray<A | B>
<A, B>(
self: NonEmptyReadonlyArray<A>,
that: Iterable<B>
): NonEmptyArray<A | B>
<A, B>(
self: Iterable<A>,
that: Iterable<B>
): Array<A | B>
}
appendAll(
self: A[]self,
const makeBy: {
<A>(f: (i: number) => A): (
n: number
) => NonEmptyArray<A>
<A>(
n: number,
f: (i: number) => A
): NonEmptyArray<A>
}
makeBy(n: numbern - self: A[]self.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length, () => fill: Tfill)
)
})