Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14140 > unrolled thread

Generators

Started by"Michael Haufe (TNO)" <tno@thenewobjective.com>
First post2012-05-24 17:41 -0700
Last post2012-05-24 18:05 -0700
Articles 2 — 1 participant

Back to article view | Back to comp.lang.javascript


Contents

  Generators "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-05-24 17:41 -0700
    Re: Generators "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-05-24 18:05 -0700

#14140 — Generators

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2012-05-24 17:41 -0700
SubjectGenerators
Message-ID<6e3d5089-590b-4894-a582-f0805a3d1b12@googlegroups.com>
I'm using JavaScript 1.8.5

I'm attempting to implement the following:

RegExp.prototype.findAll = function(str){
    var m;
    while((m = this.exec(str)))
        yield m;
}

Generator.prototype.map = function(fn,$this){
    return (fn.apply($this,value) for each(value in this));
}

Generator.prototype.toArray = function(){
    return [i for each(i in this)];
}

for use in code similar to the following:

...
tokenize: function(src){
   return this.reTokens.findAll(src).map(function(m){
...
       return token;
    },this).toArray();
}
...

The problem is that I receive the following error:

"Generator is not defined"

The code is included in the page with the following:

<script type="application/javascript;version=1.8" src="..."></script>

And in the debugger I ca do the following:

var f = this.reTokens.findAll(src);

The debugger tells me that "f" is a Generator object

The following also tells me that there is a Global Generator object:

<https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Generator>

What am I doing wrong?

[toc] | [next] | [standalone]


#14141

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2012-05-24 18:05 -0700
Message-ID<810dbe89-a99d-465d-af7e-ff012ce978df@googlegroups.com>
In reply to#14140
On Thursday, May 24, 2012 7:41:04 PM UTC-5, Michael Haufe (TNO) wrote:
> I'm using JavaScript 1.8.5
> 
> I'm attempting to implement the following:
> 
> RegExp.prototype.findAll = function(str){
>     var m;
>     while((m = this.exec(str)))
>         yield m;
> }
> 
> Generator.prototype.map = function(fn,$this){
>     return (fn.apply($this,value) for each(value in this));
> }
> 
> Generator.prototype.toArray = function(){
>     return [i for each(i in this)];
> }
> 
> for use in code similar to the following:
> 
> ...
> tokenize: function(src){
>    return this.reTokens.findAll(src).map(function(m){
> ...
>        return token;
>     },this).toArray();
> }
> ...
> 
> The problem is that I receive the following error:
> 
> "Generator is not defined"
> 
> The code is included in the page with the following:
> 
> <script type="application/javascript;version=1.8" src="..."></script>
> 
> And in the debugger I ca do the following:
> 
> var f = this.reTokens.findAll(src);
> 
> The debugger tells me that "f" is a Generator object
> 
> The following also tells me that there is a Global Generator object:
> 
> <https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Generator>
> 
> What am I doing wrong?

It looks like I can access the Generator object indirectly only:

(function(){
    var Generator = (i for(i in null)).constructor;
    Generator.prototype.map = function(fn,$this){
        for each(var i in this){
            yield fn.apply($this,i)
        }
    }

    Generator.prototype.toArray = function(){
        return [i for each(i in this)];
    }
})();

But I can't iterate over the Generator instance, hmm...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web