(n: number): <A>(a: A) => Iterable<A>
<A>(a: A, n: number): Iterable<A>Returns a Iterable containing a value repeated the specified number of times.
Details
n is normalized to an integer greater than or equal to 1.
Example (Repeating a value)
import { Iterable } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Array.from(Iterable.replicate("a", 3)), ["a", "a", "a"])export const const replicate: {
(n: number): <A>(a: A) => Iterable<A>
<A>(a: A, n: number): Iterable<A>
}
Returns a Iterable containing a value repeated the specified number of times.
Details
n is normalized to an integer greater than or equal to 1.
Example (Repeating a value)
import { Iterable } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Array.from(Iterable.replicate("a", 3)), ["a", "a", "a"])
replicate: {
(n: numbern: number): <function (type parameter) A in <A>(a: A): Iterable<A>A>(a: Aa: function (type parameter) A in <A>(a: A): Iterable<A>A) => interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(a: A): Iterable<A>A>
<function (type parameter) A in <A>(a: A, n: number): Iterable<A>A>(a: Aa: function (type parameter) A in <A>(a: A, n: number): Iterable<A>A, n: numbern: number): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(a: A, n: number): Iterable<A>A>
} = dual<(...args: Array<any>) => any, <A>(a: A, n: number) => Iterable<A>>(arity: 2, body: <A>(a: A, n: number) => Iterable<A>): ((...args: Array<any>) => any) & (<A>(a: A, n: number) => Iterable<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): Iterable<A>A>(a: Aa: function (type parameter) A in <A>(a: A, n: number): Iterable<A>A, n: numbern: number): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(a: A, n: number): Iterable<A>A> => const makeBy: <A>(
f: (i: number) => A,
options?: {
readonly length?: number
}
) => Iterable<A>
Creates an iterable by applying a function to consecutive integers.
Details
The function is called with each index starting from 0. If no length is
specified, the iterable is infinite. This is useful for generating
sequences, patterns, or any indexed data.
Example (Generating values by index)
import { Iterable } from "effect"
// Generate first 5 even numbers
const evens = Iterable.makeBy((n) => n * 2, { length: 5 })
console.log(Array.from(evens)) // [0, 2, 4, 6, 8]
// Generate squares
const squares = Iterable.makeBy((n) => n * n, { length: 4 })
console.log(Array.from(squares)) // [0, 1, 4, 9]
// Infinite sequence (be careful when consuming!)
const naturals = Iterable.makeBy((n) => n)
const first10 = Iterable.take(naturals, 10)
console.log(Array.from(first10)) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
makeBy(() => a: Aa, { length?: number | undefinedlength: n: numbern }))