Wednesday, February 22, 2006

Generators in JavaScript

My recent experience with JavaScript has been mostly positive. The tools I used for development were primitive, but the language itself has some nice features. Brendan Eich just posted about his recent work to incorporate some features from Python into JavaScript. JS is getting iterators and generators via Python!

It was an interesting opportunity to look back at the history of the generators idea. The version implemented for Python came by way of PEP 255 and an implementation by Tim Peters and Neil Schemenauer. The original idea is much older. Iterators were a feature of CLU and generators were fundamental to Icon. The CLU version was inspired by Alphard, a research language from the 1970s. There's more background on Icon's generators in Griswold's 1982 paper (which I haven't read).

Amusing historical notes:
  • When we were debating whether to use suspend or yield for the generator keyword, Tim said he'd continue to use suspend when talking to people familiar with Icon. We didn't expect that Python would become the popular point of reference for this construct.
  • Steve Majewski made the first post on generators in Python that I can find. Guido responded by saying that anything you can do with generators you can do with classes. You see the same argument todaying about allowing functions to rebind names in enclosing scopes. I find this argument a little tedious. I find the Church-Turing thesis as appealing as the next person, but I wouldn't want to write code for a Turing machine.
  • Later in that first generators thread, Tim says there are two things he doesn't want to see:

    1) Changing Python at a deep level to support coroutines/closures.

    2) Any scheme that shares with Icon that the _resumption_ of such beasts
    requires special syntax.
    He did fine with #2, but I'm not so sure about #1. Maybe you could argue that Python didn't change at a deep level, but that would be a fine argument.

No comments: