Promise (computing)
Encyclopedia
See also Lazy evaluation
Lazy evaluation
In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...



A promise is the final item in a Stream. A Stream itself is a way to shorten a very long sequence into a short one, and request only the items needed at the moment. For example, in pseudocode, assuming intseq reports an integer sequence up to its input:

intseq(5)=(1, 2, 3, 4)

For long computations, like intseq(10000), a program may not immediately need a list containing 10,000 elements. Upon a closer look at intseq, it may look something like this:

define intseq (value, this=1)
{
if (this

value)
return;
return flatten(this, intseq(value, this+1));
}

If not all integers were needed immediately, it would be more efficient to return a promise instead. A stream_intseq may look like this (in pseudocode):

define stream_intseq(value, this)
{
if (this

value)
return;
return flatten(this, "*end*", way_to_invoke(stream_intseq(value, this+1)));
}

In this way, a way to invoke (generated by way_to_invoke) the next operation is returned after a custom value that defines the end of data input (in this case, "*end*"). When needed, the caller can use the handle generated by way_to_invoke to invoke the next result. This will be added to the data part of the list, and the next invocation will be stored. For example, for stream_intseq(5), through all invocations:

(1, *end*, way_to_invoke(stream_intseq(5, 2)))
(1, 2, *end*, way_to_invoke(stream_intseq(5, 3)))
(1, 2, 3, *end*, way_to_invoke(stream_intseq(5, 4)))
(1, 2, 3, 4, *end*, way_to_invoke(stream_intseq(5, 5)))
(1, 2, 3, 4)

Notice the one extra invocation of stream_intseq, that is determined afterward to be empty. This is normal, as it happens to intseq as well.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK