### Proposal: Range Expression
The proposal based on one of the worst things in JavaScript Arrays.
If i as developer want to make sequence of some numbers or characters then
i need to use weird methods like `Array.from`, manual filling or
imperative-style array filling.
```js
const group1 = [1, 2, 3, 4, 5, ...]// manual filling
const group2= Array.from({ length: 15 }).map((_, i) => i + 1);
const group3 = [];
for (let i = 0; i < 15; i++) group3[i] = i + 1;
```
This ways to describe range sequence is huge and unreadable.
So, my proposal is: range expression which help to create lazy iterable
range sequence in declarative way.
```js
const group0 = 1...15; // from 1 to 15
const group1 = 15...1; // from 15 to 1
const group2 = 1n...15n // from 'a' to 'z'
const group3 = 'a'...'z' // from 'a' to 'z'
const group4 = 82... // lazy collection of numbers
for (const odd of 1...85) {
// ...
}
// in combination with https://github.com/tc39/proposal-iterator-helpers
const allEven = (1...100).filter(isEven);
```
So, this operator is syntax sugar for the next generator which create lazy
iterable sequence:
```js
function* range(start, end, second) {
validate(start, end, second)// type assertion of start, second and end
const step = difference(start, second); // difference calculation
let current = start;
while(isNotEnd(current, end)) { // logic for negative step and different
types
yield current;
current = next(current, step); // function which change current number
| bigint | string by difference
}
}
```
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss