Problem, as an interviewer would state it:
"Build a lazy evaluation pipeline supporting .map(), .filter(), and .take(n) over a real, potentially INFINITE sequence — transformations should only actually run on values that are genuinely consumed."
Examples:
lazySeq(infiniteNumbers()).map(n => n*2).filter(n => n%3===0).take(3); // works, despite the infinite source
Clarifying questions expected:
- Does this genuinely need to support an infinite source, or would a large-but-finite array be sufficient to demonstrate laziness?
- Should chained .map()/.filter() calls compose into ONE pass over the source, or process the whole source once per operation?
- Is there a real risk of an infinite loop if .take(n) is never reached (e.g., a filter that never matches)?
Code / implementation expected: Yes — real, direct proof against a genuinely infinite generator, counting real transformation calls to confirm the pipeline never eagerly processes more than strictly necessary.