4 ms·
Lazy evaluation works in a manner similar to generators in languages like Python. Deferring execution until it's actually asked for by enclosing the unexecuted
by Jtsummers 4y ago
Lazy evaluation works in a manner similar to generators in languages like Python. Deferring execution until it's actually asked for by enclosing the unexecuted bit in a "thunk" (Haskell term, in particular, but not the origin). Thunks can be simulated in eager languages using closures, coroutines, and continuations (among other approaches). If you've ever used futures and promises that are computed only when asked for, then you've seen lazy evaluation in an eager language. Java's Stream API makes use of lazy evaluation for the same reasons Python uses generators, deferring expensive or memory consuming actions until needed.