Created
April 28, 2012 00:06
-
-
Save jacksonwillis/2514464 to your computer and use it in GitHub Desktop.
Recursive Javascript range function
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
/* 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