(n: number): <A>(a: A) => NonEmptyArray<A>
<A>(a: A, n: number): NonEmptyArray<A>Creates a NonEmptyArray containing a value repeated n times.
When to use
Use when you need a non-empty array containing repeated copies of one value.
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 (Repeating a value)
import { Array } from "effect"
const result = Array.replicate("a", 3)
console.log(result) // ["a", "a", "a"]export const const replicate: {
(n: number): <A>(a: A) => NonEmptyArray<A>
<A>(a: A, n: number): NonEmptyArray<A>
}
Creates a NonEmptyArray containing a value repeated n times.
When to use
Use when you need a non-empty array containing repeated copies of one value.
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 (Repeating a value)
import { Array } from "effect"
const result = Array.replicate("a", 3)
console.log(result) // ["a", "a", "a"]
replicate: {
(n: numbern: number): <function (type parameter) A in <A>(a: A): NonEmptyArray<A>A>(a: Aa: function (type parameter) A in <A>(a: 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>(a: A): NonEmptyArray<A>A>
<function (type parameter) A in <A>(a: A, n: number): NonEmptyArray<A>A>(a: Aa: function (type parameter) A in <A>(a: 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>(a: A, n: number): NonEmptyArray<A>A>
} = dual<(...args: Array<any>) => any, <A>(a: A, n: number) => NonEmptyArray<A>>(arity: 2, body: <A>(a: A, n: number) => NonEmptyArray<A>): ((...args: Array<any>) => any) & (<A>(a: A, n: number) => 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>(a: A, n: number): NonEmptyArray<A>A>(a: Aa: function (type parameter) A in <A>(a: 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>(a: A, n: number): NonEmptyArray<A>A> => const makeBy: {
<A>(f: (i: number) => A): (
n: number
) => NonEmptyArray<A>
<A>(
n: number,
f: (i: number) => A
): NonEmptyArray<A>
}
makeBy(n: numbern, () => a: Aa))