Understand Lazy Evaluation in FxTS
FxTS provides lazy evaluation for
Iterable and AsyncIterable data sets. Unlike native Array methods (like .filter() or .map()) which create new arrays and traverse the entire collection at each step, FxTS lazy functions evaluate values only as needed. This allows for efficient processing of large or infinite data sets and enables early termination (e.g., using take(n)) without processing the entire source.import { pipe, range, filter, map, take, reduce } from 'fxts';
const sum = (a: number, b: number) => a + b;
pipe(
range(Infinity), // Infinite sequence
filter((a) => a % 2 === 0),
map((a) => a * a),
take(2), // Stops evaluation after 2 matches are found
reduce(sum),
); // Result: 4