<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]export const 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 = <function (type parameter) A in <A>(f: (i: number) => A, options?: {
readonly length?: number;
}): Iterable<A>
A>(f: (i: number) => Af: (i: numberi: number) => function (type parameter) A in <A>(f: (i: number) => A, options?: {
readonly length?: number;
}): Iterable<A>
A, options: | {
readonly length?: number
}
| undefined
options?: {
readonly length?: number | undefinedlength?: number
}): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(f: (i: number) => A, options?: {
readonly length?: number;
}): Iterable<A>
A> => {
const const max: numbermax = options: | {
readonly length?: number
}
| undefined
options?.length?: number | undefinedlength !== var undefinedundefined ? 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(options: {
readonly length?: number
}
options.length?: numberlength)) : var Infinity: numberInfinity
return {
[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]() {
let let i: numberi = 0
return {
Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next(): type IteratorResult<T, TReturn = any> =
| IteratorYieldResult<T>
| IteratorReturnResult<TReturn>
IteratorResult<function (type parameter) A in <A>(f: (i: number) => A, options?: {
readonly length?: number;
}): Iterable<A>
A> {
if (let i: numberi < const max: numbermax) {
return { IteratorYieldResult<A>.value: Avalue: f: (i: number) => Af(let i: numberi++), IteratorYieldResult<TYield>.done?: false | undefineddone: false }
}
return { IteratorReturnResult<TReturn>.done: truedone: true, IteratorReturnResult<any>.value: anyvalue: var undefinedundefined }
}
}
}
}
}