Skip to content

Instantly share code, notes, and snippets.

Created January 14, 2010 06:53
Show Gist options
  • Save anonymous/276962 to your computer and use it in GitHub Desktop.
Save anonymous/276962 to your computer and use it in GitHub Desktop.
function LazySeq(fn, start) {
var last;
//functions to apply on realization
var maps = [];
//use lazy function definition
this.next = function() {
this.next = function() {
return last = fn(last);
};
return last = start;
};
this.map = function(fn) {
maps.push(fn);
return this;
};
this.nth = function(n) {
while(n-- > 0) this.next();
return maps.reduce(this.next())(
function(xs,y) { return y(xs); });
};
//'realize' elements
this.take = function(n) {
var tmpArr = [];
while(n--)
tmpArr.push(maps.reduce(this.next())(
function(xs,y) { return y(xs); })
);
return tmpArr;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment