What's the difference between a promise
or future
and a lazily evaluated function/object
?
They both act like a placeholder for a deferred computation, and I understand that the principles that they operate on are different, but what's the actual difference?
In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing).
Futures and promises are pretty similar concepts, the difference is that a future is a read-only container for a result that does not yet exist, while a promise can be written (normally only once).
Lazy evaluation's is not always better. The performance benefits of lazy evaluation can be great, but it is not hard to avoid most unnecessary evaluation in eager environments- surely lazy makes it easy and complete, but rarely is unnecessary evaluation in code a major problem.
Lazy evaluation is not, in general, faster.
Both promises and "lazily evaluated objects" are forms of proxies. Proxy is the key word here. A proxy means "a placeholder for an actual object value"
A promise is a time constrained (temporal) proxy for a value. Some libraries like Kris Kowal's Q-Connection even let you proxy remote object with promises, and there is a lot of fascinating research (really!) about this use case in hostile conditions made by Mark Miller. This was actually the original motivation for promises (to deal with network latency).
A lazy evaluated object is a proxy for a value to postpone evaluation to the point you have to do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With