JavaScript 1.7 yield

Discussion in 'Javascript' started by Kaleb Hornsby, Apr 21, 2009.

  1. 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???
    g.send(2); // 5
    g.send(2); // 6
    Kaleb Hornsby, Apr 21, 2009
    #1
    1. Advertising

  2. Kaleb Hornsby wrote:
    > 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
    Thomas 'PointedEars' Lahn, Apr 21, 2009
    #2
    1. Advertising

  3. Yes, I have read this specification and have been using the
    spidermonkey javascript engine to try and implement my code. I can get
    the .next() and .close() methods to work, but I have no idea how to
    get the .send() method to do what I think it is supposed to do.
    Kaleb Hornsby, Apr 21, 2009
    #3
  4. Kaleb Hornsby wrote:
    > I am having difficulty finding a working example of the yield
    > statement's .send() method in action.


    See answer in the Mozilla mozilla.dev.tech.js-engine group where you
    posted too.

    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
    Martin Honnen, Apr 21, 2009
    #4
  5. Kaleb Hornsby wrote:
    > Yes,


    Yes -- to what of what I posted?

    > I have read this specification and have been using the
    > spidermonkey javascript engine to try and implement my code. I can get
    > the .next() and .close() methods to work, but I have no idea how to
    > get the .send() method to do what I think it is supposed to do.


    If it's actually borken, you can't until the fix is checked in.


    PointedEars
    Thomas 'PointedEars' Lahn, Apr 21, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick
    Replies:
    2
    Views:
    2,487
    Roedy Green
    Oct 4, 2003
  2. Replies:
    1
    Views:
    320
    Gabriel Genellina
    Apr 22, 2008
  3. Markus
    Replies:
    1
    Views:
    196
    Mark Hubbart
    Sep 27, 2004
  4. Michael Edgar
    Replies:
    13
    Views:
    271
    Brian Candler
    Apr 21, 2011
  5. VK
    Replies:
    1
    Views:
    85
    Thomas 'PointedEars' Lahn
    Nov 29, 2007
Loading...

Share This Page