Knowledgebase

Iterators, Generators and Lazy Evaluation Print

  • programminglanguages, programming, errors, guide, howto, solution, zillionkinghost, hosting
  • 0

Processing sequences efficiently.

WHAT AN ITERATOR IS

Something producing values one at a time, on request.

WHY THAT MATTERS

The whole sequence need not exist in memory.

WHAT A GENERATOR IS

A function producing values lazily, resuming where it left off.

WHAT LAZY EVALUATION MEANS

Work performed only when the result is needed.

WHAT IT ENABLES

Processing sequences larger than memory Infinite sequences Avoiding computing values never used

WHAT THAT LOOKS LIKE PRACTICALLY

Reading a very large file line by line rather than loading it entirely.

WHY THAT MATTERS

Loading a large file entirely exhausts memory, and the failure appears only with real data.

WHAT TO BE CAREFUL WITH

Iterators that can be consumed only once Lazy sequences where the underlying source has closed Chained operations obscuring how much work occurs

THAT SECOND POINT

A common error: returning a lazy sequence from a function that closes the file.

WHAT TO DO ABOUT IT

Materialise it before the source closes, or keep the source open.

WHAT TO PREFER FOR LARGE DATA

Streaming, always.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot