Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
EdSaleh opened this issue May 12, 2019 · 7 comments
Closed
Labels
feature suggestion Please see https://github.com/tc39/ecma262/blob/HEAD/CONTRIBUTING.md#creating-a-new-proposal

Comments

@EdSaleh
Copy link

EdSaleh commented May 12, 2019

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,

@EdSaleh EdSaleh changed the title Add range function iterator to be used with for loop Add 'range(start, end, step)' iterator function to be used with for loop May 12, 2019
@ljharb
Copy link
Member

ljharb commented May 12, 2019

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).

@ljharb ljharb closed this as completed May 12, 2019
@ljharb ljharb added the feature suggestion Please see https://github.com/tc39/ecma262/blob/HEAD/CONTRIBUTING.md#creating-a-new-proposal label May 12, 2019
@EdSaleh
Copy link
Author

EdSaleh commented May 12, 2019

@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.

@ljharb
Copy link
Member

ljharb commented May 12, 2019

function* range(a, b, s = 1) { for (let i = a; i < b; i += s) { yield i; } } works for that as well :-)

@EdSaleh
Copy link
Author

EdSaleh commented May 12, 2019

Agree. Hope this is implemented into JavaScript in the future.

@Pauan
Copy link

Pauan commented May 12, 2019

@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.

@EdSaleh
Copy link
Author

EdSaleh commented May 12, 2019

Yes, I know. I'm talking about the range() method to be built-in into JavaScript.

@josephrocca
Copy link

josephrocca commented May 28, 2019

@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

tc39/proposal-slice-notation#19 (comment)

That last one looks pretty neat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature suggestion Please see https://github.com/tc39/ecma262/blob/HEAD/CONTRIBUTING.md#creating-a-new-proposal
Projects
None yet
Development

No branches or pull requests

4 participants