Modal webapps, iterators and beyond

Excellent, easy to understand for old fashioned programmers, the introductory lecture that Sam Ruby does about continuations, Continuations for Curmudgeons.

Brian McCallister writes about guidelines to write webapps, including a reference to the concept Modal Webapps, what Stefano has been doing for years using continuations first with scheme and later with javascript.

By coincidence, I have been preparing my lesson on iterators for the University classes about the time I was reading those entries. I decided to give the students the simple example about generators that Sam gives, slightly changed to put the generator's end inside the generator itself:

def fib(max=1000):
  """Devuelve un generador para n. de Fibonacci"""
  yield 0
  i,j = 0,1
  while j<max:
    yield j
    i,j = j,i+j
  raise StopIteration

if __name__ == "__main__": 
  for n in fib():
    print n
This compares very naturally with the implementation as java Iterators of the same code, except that java types get often in the way, and it is fairly more verbose:
import java.util.Iterator;

public class Fibonacci implements Iterator {

	private int i=0;
	private int j=1;
	private int max;
		
	public Fibonacci(int max) {
		this.max = max;
	}
	
	public Fibonacci() {
		this(1000);
	}

	public boolean hasNext() {
		return j < max;
	}

	public Object next() {
		Integer result = new Integer(j);
		int temp = i;
		i = j;
		j = j + temp;
		return result;
	}

	public void remove() {
		throw new UnsupportedOperationException("No soportado");
	}

	public static void main(String[] args) {
		Iterator it = new Fibonacci();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
}

Both python generators and java iterators would allow us to write portlets in a very natural way, except that the back button would be forbidden. But it is nonetheless mostly unusable in portlets anyway, so it is not a big lose.

I'm waiting for jython to have generators (i.e, to be 2.3) to try an embedded jython interpreter to write portlets. I'm thinking that, while this comes, writing a HttpPortletRequestIterator and toying how easy it is to write portlets using it would be a win.