Skip to content

Instantly share code, notes, and snippets.

@shuding
Created January 22, 2016 07:37
Show Gist options
  • Save shuding/6b8fb023f63fc8fb4f94 to your computer and use it in GitHub Desktop.
Save shuding/6b8fb023f63fc8fb4f94 to your computer and use it in GitHub Desktop.
An one-line range function with JavaScript ES6, like Python
const range = (s, t = 0, a = 1) => [...Array(Math.abs(s - t))].map((x, i) => Math.min(s, t) + i * a).filter(x => x < Math.max(s, t));
console.log(range(5));
console.log(range(2, 5));
console.log(range(0, 5, 2));
// node.js: please run with `node --harmony_default_parameters --harmony range.js`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment