-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfor_each.ts
More file actions
29 lines (29 loc) · 802 Bytes
/
for_each.ts
File metadata and controls
29 lines (29 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Calls a function for each value in an iterable.
*
* Use {@linkcode https://jsr.io/@core/iterutil/doc/map/~/map map} to transform values.
* Use {@linkcode https://jsr.io/@core/iterutil/doc/filter/~/filter filter} to filter values.
* Use {@linkcode https://jsr.io/@core/iterutil/doc/async/for-each/~/forEach forEach} to iterate asynchronously.
*
* @param iterable The iterable to iterate over.
* @param fn The function to call for each value.
*
* @example
* ```ts
* import { forEach } from "@core/iterutil/for-each";
*
* forEach([1, 2, 3], (v) => console.log(v));
* // 1
* // 2
* // 3
* ```
*/
export function forEach<T>(
iterable: Iterable<T>,
fn: (value: T, index: number) => void,
): void {
let index = 0;
for (const value of iterable) {
fn(value, index++);
}
}