Comparison
GeneratorvsLazy evaluation
Generator
the function paused in the middle, handed back a value, and carried on from that exact line when asked for the next.
A function that produces a sequence one element at a time, suspending its own execution between them. It is an iterator you write as ordinary straight-line code, which is what makes it so much easier to get right than a hand-written cursor. It is also how a sequence of unbounded or expensive elements becomes practical, because nothing beyond the current element is computed.
Full entry →Lazy evaluation
you built a pipeline over a million rows, took the first ten, and only about ten rows were ever processed.
Deferring computation until its result is needed, and not computing it at all if it never is. It makes infinite sequences expressible, makes early-exit pipelines cheap, and makes performance depend on when a value is forced rather than on where it is written. That last part is also its cost: in a lazy pipeline, exceptions and effects happen somewhere other than where they appear to.
Full entry →