lazy evaluation
Postponing work until its result is actually needed, rather than doing it all in advance.
In more detail
Lazy code can skip work whose result is never used, and it can handle very large sequences with little memory because values are produced one at a time. Examples include Python generators, Java streams and loading related database records only when they are first accessed. The cost is that a lazy sequence can often be consumed only once.
Related terms
- Stream API — The standard-library toolkit for describing a sequence of data-processing steps, such as filter, map and collect, over a source like a list.
- intermediate operation — A stream step, such as filter, map, sorted or distinct, that returns another stream and does no work until a terminal operation runs.
- lazy loading — Loading related data from the database only when code first accesses it, instead of together with the object that refers to it.
- class loading — The JVM finding a class's bytecode on the class path, reading it and preparing it the first time the class is needed.
- generator — A function that can pause and resume, producing a series of values one at a time with yield, each only when it is requested.
- iterator — An object that produces the items of a sequence one at a time, on request, instead of all at once.
- range() — A built-in that produces a sequence of whole numbers on demand, such as range(5) for 0, 1, 2, 3, 4.
Taught in
- The Streams API — Modern Java: Virtual Threads & High-Throughput Services
- Lists, Range And The For Loop — Hands-on Python: Complete Video Course & Book
- Comprehensions — Hands-on Python: Complete Video Course & Book
- Iterators And Generators — Hands-on Python: Complete Video Course & Book