Ecma262: Add 'range(start, end, step)' iterator function to be used with for loop

Created on 12 May 2019  路  7Comments  路  Source: tc39/ecma262

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,

feature suggestion

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.

All 7 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wangyi7099 picture wangyi7099  路  5Comments

bkardell picture bkardell  路  3Comments

renanbastos93 picture renanbastos93  路  4Comments

AlexSHoffman picture AlexSHoffman  路  3Comments

ursi picture ursi  路  4Comments