<A>(f: (a: A, i: number) => void): (self: Iterable<A>) => void
<A>(self: Iterable<A>, f: (a: A, i: number) => void): voidIterates over the Iterable, applying f to each element.
Example (Iterating with side effects)
import { Iterable } from "effect"
// Print each element
const numbers = [1, 2, 3, 4, 5]
Iterable.forEach(numbers, (n) => console.log(n))
// Prints: 1, 2, 3, 4, 5
// Use index in the callback
const letters = ["a", "b", "c"]
Iterable.forEach(letters, (letter, i) => {
console.log(`${i}: ${letter}`)
})
// Prints: "0: a", "1: b", "2: c"
// Side effects with any iterable
const results: Array<number> = []
Iterable.forEach(Iterable.range(1, 5), (n) => {
results.push(n * n)
})
console.log(results) // [1, 4, 9, 16, 25]
// Process in chunks
const data = Iterable.chunksOf([1, 2, 3, 4, 5, 6], 2)
Iterable.forEach(data, (chunk) => {
console.log(`Processing chunk: ${Array.from(chunk)}`)
})
// Prints: "Processing chunk: 1,2", "Processing chunk: 3,4", "Processing chunk: 5,6"export const const forEach: {
<A>(f: (a: A, i: number) => void): (
self: Iterable<A>
) => void
<A>(
self: Iterable<A>,
f: (a: A, i: number) => void
): void
}
Iterates over the Iterable, applying f to each element.
Example (Iterating with side effects)
import { Iterable } from "effect"
// Print each element
const numbers = [1, 2, 3, 4, 5]
Iterable.forEach(numbers, (n) => console.log(n))
// Prints: 1, 2, 3, 4, 5
// Use index in the callback
const letters = ["a", "b", "c"]
Iterable.forEach(letters, (letter, i) => {
console.log(`${i}: ${letter}`)
})
// Prints: "0: a", "1: b", "2: c"
// Side effects with any iterable
const results: Array<number> = []
Iterable.forEach(Iterable.range(1, 5), (n) => {
results.push(n * n)
})
console.log(results) // [1, 4, 9, 16, 25]
// Process in chunks
const data = Iterable.chunksOf([1, 2, 3, 4, 5, 6], 2)
Iterable.forEach(data, (chunk) => {
console.log(`Processing chunk: ${Array.from(chunk)}`)
})
// Prints: "Processing chunk: 1,2", "Processing chunk: 3,4", "Processing chunk: 5,6"
forEach: {
<function (type parameter) A in <A>(f: (a: A, i: number) => void): (self: Iterable<A>) => voidA>(f: (a: A, i: number) => voidf: (a: Aa: function (type parameter) A in <A>(f: (a: A, i: number) => void): (self: Iterable<A>) => voidA, i: numberi: number) => void): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(f: (a: A, i: number) => void): (self: Iterable<A>) => voidA>) => void
<function (type parameter) A in <A>(self: Iterable<A>, f: (a: A, i: number) => void): voidA>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, f: (a: A, i: number) => void): voidA>, f: (a: A, i: number) => voidf: (a: Aa: function (type parameter) A in <A>(self: Iterable<A>, f: (a: A, i: number) => void): voidA, i: numberi: number) => void): void
} = dual<(...args: Array<any>) => any, <A>(self: Iterable<A>, f: (a: A, i: number) => void) => void>(arity: 2, body: <A>(self: Iterable<A>, f: (a: A, i: number) => void) => void): ((...args: Array<any>) => any) & (<A>(self: Iterable<A>, f: (a: A, i: number) => void) => void) (+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>(self: Iterable<A>, f: (a: A, i: number) => void): voidA>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, f: (a: A, i: number) => void): voidA>, f: (a: A, i: number) => voidf: (a: Aa: function (type parameter) A in <A>(self: Iterable<A>, f: (a: A, i: number) => void): voidA, i: numberi: number) => void): void => {
let let i: numberi = 0
for (const const a: Aa of self: Iterable<A>self) {
f: (a: A, i: number) => voidf(const a: Aa, let i: numberi++)
}
})