Skip to content

Instantly share code, notes, and snippets.

@kpuputti
Last active December 15, 2015 02:09
Show Gist options
  • Save kpuputti/5185688 to your computer and use it in GitHub Desktop.
Save kpuputti/5185688 to your computer and use it in GitHub Desktop.
// range0(n) -> [0, ... , (n - 1)]
function range0(n) {
return Array.apply(null, Array(n)).map(function (val, i) {
return i;
});
}
// range(end) -> [0, ... , (end - 1)]
// or
// range(start, end) -> [start, ... , (end - 1)]
function range() {
if (arguments.length === 1) {
return range0(arguments[0]);
} else if (arguments.length === 2) {
return range0(arguments[1]).slice(arguments[0]);
} else {
throw new Error('Bad arguments.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment