Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between lazy evaluation and promises/futures

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?

like image 507
Pedro Montoto García Avatar asked May 20 '14 15:05

Pedro Montoto García


People also ask

What is lazy evaluation?

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).

Are futures and promises the same?

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).

Is lazy evaluation always better?

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.

Are lazy evaluations faster?

Lazy evaluation is not, in general, faster.


1 Answers

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.

like image 179
Benjamin Gruenbaum Avatar answered Oct 28 '22 08:10

Benjamin Gruenbaum