Hello,
I am proposing the addition of range(start, end, step=1) iterator function.
This function would make creating for loops easy and follow the trend going on with other programming languages replacing for(;;) style.
Example:
Current way of declaring for loop:
for(let i = 0;i<10;i++){}
New way with range iterator function:
for(let i of range(0,9)){}
Thank You,
Please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#creating-a-new-proposal for how to best suggest new features for the language.
For some prior art, see http://array.build, and Array.from({ length: 9 }, (_, i) => i).
@Ijharb, thanks for letting me know.
I would avoid using arrays for creating a for loop since that would create an unnecessary array into the memory. It would be more efficient if an iterator instead.
function* range(a, b, s = 1) { for (let i = a; i < b; i += s) { yield i; } } works for that as well :-)
Agree. Hope this is implemented into JavaScript in the future.
@EdSaleh To be clear, generators are already a part of JavaScript, and are implemented in every browser except Internet Explorer. You don't need to wait for anything, you can use it today.
Yes, I know. I'm talking about the range() method to be built-in into JavaScript.
@ljharb Perhaps the template for a feature request (when you submit an issue to this repo) could direct people to esdiscuss.org? That seems like the best "first step" for people who are interested in proposing a new feature.
https://esdiscuss.org/topic/new-proposal-number-range-yes-range-again
https://github.com/Jack-Works/proposal-Number.range
https://github.com/tc39/proposal-slice-notation/issues/19#issuecomment-421453934
That last one looks pretty neat.
Most helpful comment
@ljharb Perhaps the template for a feature request (when you submit an issue to this repo) could direct people to esdiscuss.org? That seems like the best "first step" for people who are interested in proposing a new feature.
https://esdiscuss.org/topic/new-proposal-number-range-yes-range-again
https://github.com/Jack-Works/proposal-Number.range
https://github.com/tc39/proposal-slice-notation/issues/19#issuecomment-421453934
That last one looks pretty neat.