Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created October 17, 2012 13:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abozhilov/3905489 to your computer and use it in GitHub Desktop.
Save abozhilov/3905489 to your computer and use it in GitHub Desktop.
range
function range(start, end, step) {
step = Math.floor(Math.abs(step)) || 1;
if (typeof end == 'undefined') {
end = start;
start = 0;
}
else if (start > end) {
var s = start;
start = end;
end = s;
}
return function (fn) {
for (var i = start; i < end; i += step) {
fn(i);
}
};
}
//Examples
range(10)(function (x) {
console.log(x); // 0..9
});
range(10, 20)(function (x) {
console.log(x); // 10..19
});
range(10, 20, 2)(function (x) {
console.log(x); // 10, 12 .. 18
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment