Kaleb said:
I am having difficulty finding a working example of the yield
statement's .send() method in action.
This is what I have:
var f = function(x) {
var i = isNaN(x)? 0: x;
while(true) {
yield i++;
}
}
g = f();
g.next(); // 0
g.next(); // 1
g.next(); // 2
g.next(); // 3
g.send(2); // 4 should these return 2???
I'm not sure about that, the specification of the method is rather vague on
that:
,-<
https://developer.mozilla.org/en/New_in_JavaScript_1.7#Resuming_a_generator_at_a_specific_point>
|
| Once a generator has been started by calling its next() method,
| you can use send(), passing a specific value that will be treated
| as the result of the last yield. The generator will then return
| the operand of the subsequent yield.
However, the next generator.next() call should definitely return 3 if the
generator.send() method worked as described, and it doesn't. So it appears
that generator.send() is borken in Firefox 3(.0.6 for Linux), that is, it
works as if its argument is ignored. Barring further corrections, you
should file a bug or vote for an existing one (I don't know one, but I'm not
as up-to-date on Bugzilla as before.)
BTW, this is how I tested it in Firebug 1.3X3 which does not appear to
provide means to define the JavaScript version for the console (Joe
[Hewitt], please add that feature, like version(170)! TIA):
var s = document.createElement("script");
s.type = "text/javascript; version=1.7";
s.appendChild(document.createTextNode(<xml>
<![CDATA[
var f = function(x) {
var i = isNaN(x) ? 0 : x;active
while (true)
{
yield i++;
}
};
function bar()
{
try
{
var g = f();
/* 0 */
console.log(g.next());
/* 1 */
console.log(g.next());
/* 2 */
console.log(g.next());
/* 3 */
console.log(g.send(1));
/* yields 4, not 2 */
console.log(g.next());
}
catch (e)
{
/* not thrown */
e
}
}
]]>
</xml>));
document.body.appendChild(s);
bar();
PointedEars