Skip to content

Instantly share code, notes, and snippets.

@jacksonwillis
Created April 28, 2012 00:06
Show Gist options
  • Save jacksonwillis/2514464 to your computer and use it in GitHub Desktop.
Save jacksonwillis/2514464 to your computer and use it in GitHub Desktop.
Recursive Javascript range function
/* recursive version
*/
var M = function (f) { return f(f); } // Mockingbird function
range = M(function (f) {
return function (min, max) {
return min === max ? [min] : [min].concat(f(f)(min + 1, max));
}
});
/* normal version
*/
range = function (min, max) { var i, res = []; for (i = min; i <= max; ++i) { res.push(i); } return res; }
/* usage
*/
range(3, 10); //=> [3, 4, 5, 6, 7, 8, 9, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment