Skip to content

Instantly share code, notes, and snippets.

@fabriceleal
Created November 22, 2012 21:45
Show Gist options
  • Save fabriceleal/4133039 to your computer and use it in GitHub Desktop.
Save fabriceleal/4133039 to your computer and use it in GitHub Desktop.
functional repeat
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;
};
})());
@fabriceleal
Copy link
Author

executes a function number times, returns an array with the results

@fabriceleal
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment