Created
November 22, 2012 21:45
-
-
Save fabriceleal/4133039 to your computer and use it in GitHub Desktop.
functional repeat
This file contains 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
Number.prototype.repeat = function(doSomething){ | |
var last = Math.floor(this); | |
var ret = new Array(last); | |
for(var i = 0; i < last; ++i){ | |
ret[i] = doSomething(); | |
} | |
return ret; | |
}; | |
// Example: | |
(5).repeat((function(){ | |
var i = 0; | |
return function(){ | |
++i; | |
console.log(i); | |
return i; | |
}; | |
})()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
executes a function number times, returns an array with the results