<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"elements
Source effect/Iterable.ts:21549 lines
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
} = import dualdual(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++)
}
})