Ranges

kdex kdex at kdex.de
Thu Nov 3 12:10:48 UTC 2016


A few months ago, there was a thread on ESDiscuss [1] where somebody asked for native `Range` support,
i.e. numeric ranges, string ranges, etc.

It seems like it didn't receive much attention, so I'd like to bring this up for discussion again.

If you were to iterate from X to Y, you would usually use a `for` loop that uses a loop variable `i`,
which you would declare with `let`. You can't use `const`, since you'll have to increment it, and you
don't want to use `var`, because it makes no sense to make a reference to `i` outside of the loop,
unless you were to re-use the loop variable for further loops.

In other words, you're forced to choose a scope where other scopes barely make any sense.
Having a for-loop that abstracts over this needless explicitness could help make the concept of
iteration more declarative.

Python has `range()` (but no for loops that create ranges) [2], and Scala introduced two for-loop syntaxes
that will create ranges for you:

```scala
for (i <- 1 to 10) { … } // 10 inclusive
for (i <- 1 until 10) { … } // 10 exclusive
```

There's a plethora of npm packages that implement ranges, the most popular one apparently being `fill-range`
with 7.4 million downloads a month, so it seems that the community would appreciate native support a lot.

I've searched through some proposals, but couldn't find anyone currently writing a spec for it.
Is there general interest for ranges at TC39? It struck me as odd that the only thing I found about them in ES was [1].

[1] https://esdiscuss.org/topic/feature-request-add-range-type-data
[2] https://docs.python.org/3/library/functions.html#func-range
[3] https://www.npmjs.com/package/fill-range


More information about the es-discuss mailing list