Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Use Continuations to Develop Complex Web Applications

By Abhijit Belapurkar
2005-04-22


What is a Continuation, Anyway?

A continuation is traditionally defined as a function representing "the rest of the computation" or "what to do next." In other words, sending the intermediate result (generated by the preceding computation) to a continuation should yield the final result of the overall computation.

Consider, for example, the following rudimentary Java method that returns the square of the integer that is passed to it:

Listing 1. Method to compute square of integer input

public static int computeSquare(int x)

{
return (x*x);
}
This method returns a value, but leaves implicit the location to which the value should be returned. A continuation, properly applied, would make the return location explicit.

So, suppose I change the above method -- and every other method in the system -- to include an additional argument representing a continuation. Typically, this would be the last argument following all the other arguments to the method. When the function is called, it performs its internal logic as before; only, instead of returning the output value, it continues with the output value, by passing the value on to the continuation and asking the computation to resume. Thus, the above method would be rewritten as shown in Listing 2:

Listing 2. Method rewritten using a continuation object

public static int computeSquare(int x, Continuation c)

{
c.evaluate(x*x);
}
This programming style, in which no function is ever allowed to return, is called Continuation Passing Style, or CPS. A function, f1, emulates returning by passing its would-be return value to a continuation function that must have been explicitly passed to it. Similarly, if f1 needs to call a second function, f2, in the middle, it must pass to f2 (along with the rest of the arguments) a continuation representing the "rest of f1." Once f2 is finished, the continuation "rest of f1" is resumed with the output of f2's computation.

Now, to add a little twist, I'll bring in another function, f3, which is called at the tail end of f2. If f2 were to follow f1's suit in terms of passing its continuation, it would end up passing only the continuation of f1 to f3. Resuming the continuation of f1 would be all that would remain once f3 had been executed.

Said another way, a continuation is a saved snapshot of the executable state of a program at any given point in time. It is possible to restore this state and restart the execution of the program from that point onward, such that the stack trace, all the local variables, and the program counter can reclaim their old values. See Resources to learn more about continuations. Now I'll focus on showing you what they can do to simplify your programming efforts in complex Web applications. Before we get into that, let me take a moment to further explain the problems I'm trying to address.

Higher order functions
Note that in the example of Listing 2, the function computeSquare is essentially a higher order function, because it takes a functor object (the Continuation object, c) as parameter and sends a message to the evaluate method of the functor with the intermediate result generated by computing x*x.

Tutorial Pages:
» A Programming Paradigm to Simplify MVC for the Web
» What is a Continuation, Anyway?
» Problems in Conventional Web development
» The Case for Continuations
» User-Centered Navigation
» You Make the Rules!
» The Continuations Repository
» An Example Application
» Web Continuations in Apache Cocoon
» The Application Sitemap
» The Application Logic
» Understanding the Application Logic
» Resuming the Continuation
» JavaScript vs. the Java language
» Continuations in Java code
» Pros and Cons of Continuations
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources