Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I just don't get continuations!

What are they and what are they good for?

I do not have a CS degree and my background is VB6 -> ASP -> ASP.NET/C#. Can anyone explain it in a clear and concise manner?

like image 698
Oded Avatar asked Sep 02 '08 20:09

Oded


2 Answers

Imagine if every single line in your program was a separate function. Each accepts, as a parameter, the next line/function to execute.

Using this model, you can "pause" execution at any line and continue it later. You can also do inventive things like temporarily hop up the execution stack to retrieve a value, or save the current execution state to a database to retrieve later.

like image 169
John Millikin Avatar answered Sep 28 '22 16:09

John Millikin


You probably understand them better than you think you did.

Exceptions are an example of "upward-only" continuations. They allow code deep down the stack to call up to an exception handler to indicate a problem.

Python example:

try:     broken_function() except SomeException:     # jump to here     pass  def broken_function():     raise SomeException() # go back up the stack     # stuff that won't be evaluated 

Generators are examples of "downward-only" continuations. They allow code to reenter a loop, for example, to create new values.

Python example:

def sequence_generator(i=1):     while True:         yield i  # "return" this value, and come back here for the next         i = i + 1  g = sequence_generator() while True:     print g.next() 

In both cases, these had to be added to the language specifically whereas in a language with continuations, the programmer can create these things where they're not available.

like image 29
Dustin Avatar answered Sep 28 '22 14:09

Dustin