javascript / expert
Snippet
Generator-Based Data Transformation Pipelines
Generators allow for lazy evaluation of large datasets. Instead of creating intermediate arrays for every operation, each item is processed through the entire pipeline one by one, significantly reducing memory overhead for heavy data processing tasks.
snippet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
function* filter<T>(items: Iterable<T>, predicate: (i: T) => boolean) {for (const item of items) {if (predicate(item)) yield item;}}function* map<T, U>(items: Iterable<T>, fn: (i: T) => U) {for (const item of items) {yield fn(item);}}const pipeline = map(filter([1, 2, 3, 4], x => x % 2 === 0), x => x * 10);
nextjs
Breakdown
1
function* filter<T>(items: Iterable<T>...
Defines a generator function that produces values on-demand.
2
yield item;
Pauses the generator and returns the current value to the consumer.
3
const pipeline = map(filter(...));
Composes lazy operations without executing them until the pipeline is iterated.