jweinst1
11/11/2015 - 8:24 AM

implementation of a generator and simple int range function in js

implementation of a generator and simple int range function in js

//generator implementation of integers
var generator = function() {
    this.i = 0;
    this.next = function() {
        var elem = this.i;
        this.i += 1;
        return elem;
    };
    this.reset = function() {
        this.i = 0;
    };
};
//Generates a one member list of a int array
var list_gen = function() {
    this.lst = [0];
    this.next = function() {
        var elem = this.lst.slice();
        this.lst[0] += 1;
        return elem;
    };
};
/* Creates an integer array from 0 to n-1:
   int_array(9)
=> [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]*/
function int_array(n) {
    gen = new list_gen();
    var numlst = gen.next();
    while (numlst.length<n) {
        numlst = numlst.concat(gen.next());
    }
    return numlst;
}