On Yields, Generators, and Iterators

by Kurt Cagle

One of the things that I so enjoy about watching the Mozilla Firefox development process is that they are not shy in pushing forward with technologies that many would have thought solid and immutable. JavaScript is a case in point. JavaScript had largely stagnated as a language from about 1998 on, until AJAX (spurred largely by renewed interest in the Mozilla platform) suddenly came out of nowhere. Accessors (getters and setters), more sophisticated array handling, E4X and other innovations have come out of the development process.

With the release of Firefox 2.0 beta 1, there are several new features that have been dropped into the language, features which are likely to be used sparingly at first but which offer a significant set of capabilities that will likely become welcome tools in any AJAX developer's toolbox.


10 Comments

Mark
2006-07-24 01:36:49
I'm only up to the first code example in "Firing up the Generator" but this concept of yeild doesn't look disimilar to a none blocking real time exmaple, but you are manually polling to see if your condition is set which you check by using the "internal next()" method. Is that correct or am I completely off the mark ?
Sylvain Hellegouarch
2006-07-24 03:25:26
Kurt,


You should definitely dig into Python which has offered those features for a little while now :)

Victoria
2006-07-24 10:09:44
Good post!!
adam
2006-07-24 15:18:42
So, I hate to be all practical, but I have to ask, which other browsers support Javascript 1.7?
Mark Aufflick
2006-07-24 20:32:44
As per Sylvain, this is hardly a new construct. In fact one could assume that this syntax is inspired by the identical "yield" syntax in Ruby.
Kurt Cagle
2006-07-24 21:52:14
Thanks for the comments - yes, yield has a great deal of similarity to such structures in both Ruby and Python, and also borrows more than a little bit from Haskell monads. The languages are feeding off one another, pushing in a direction that I find quite interesting: structural similarities without the need for an underlying CLR.
Sylvain Hellegouarch
2006-07-25 03:56:52
@Mark: Apology if my comment implied that Python had someway invented this feature. I didn't mean that :)
Sylvain Hellegouarch
2006-07-25 04:35:53
Ha!


I do realise I did not read correctly Mark's comment.


Forget about those messages please.

Arthur Browning
2006-07-26 03:55:02
Good article and good news for better designs. Anybody hear pros or recent cons to Mambo website designs, limitations, and /or coding problems.
Don
2006-12-13 01:27:44
Can yield keyword can be used inside a recusrsive function? Below is not working:


function calculate_cube3(func, start, end, incr)
{
var i = start;
yield [i, func(i)];
if (start <= end)
{
arguments.callee(func, start+incr, end, incr);
}
alert(i);
}
var first = calculate_cube3(cube, 0, 10, 3);
document.write("Print cubes of numbers (demonstrating multiple yield statement): ");
try {
while (true)
{
var [num, num_cube] = first.next();
document.write("[" + num + ", " + num_cube + "] ");
}
}
catch (err if err instanceof StopIteration) {
document.write("
\n
\n");
}