Created
January 14, 2010 06:53
-
-
Save anonymous/276962 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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