Ranges

classic Classic list List threaded Threaded
42 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Ranges

kdex
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
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Hikaru Nakashima
How about this

```
for ( i of Array.range(1, 10) ) { ... }
// OR
for ( i of [1..10] )  { ... }
```



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Isiah Meadows-2

I'll note, just for clarity, that Scala's `1 to 10` is technically just a normal method call equivalent to `(1).to(10)`, with optional parentheses removed.

Also, I'd prefer this to be a generator instead, so infinite ranges are also possible, and so it doesn't have to be eager.


On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]> wrote:
How about this

```
for ( i of Array.range(1, 10) ) { ... }
// OR
for ( i of [1..10] )  { ... }
```


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

kdex
Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just return a generator
or at least something that extends a generator, though. I wonder if it's viable to implement
something akin to `.length` on ranges, which could be natural numbers or `Infinity`.

As for numbers, I don't see any issues. One issue that came up in the original thread was
that string ranges may need a better definition, as ["A".."C"] might not necessarily transpile
to be a generator that yields "A", "B" and "C".

On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:

> I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> normal method call equivalent to `(1).to(10)`, with optional parentheses
> removed.
>
> Also, I'd prefer this to be a generator instead, so infinite ranges are
> also possible, and so it doesn't have to be eager.
>
> On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> wrote:
>
> > How about this
> >
> > ```
> > for ( i of Array.range(1, 10) ) { ... }
> > // OR
> > for ( i of [1..10] )  { ... }
> > ```
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Isiah Meadows-2

If string ranges are based on character codes, it will (for the Latin alphabet, at least, not necessarily for other languages).

I would prefer a function over syntax, though, since it would be more easily adopted (polyfill > syntax), and it would fit more idiomatically with the rest of the language (which also uses functions for most utilities).

Maybe a `Number.range` would work?

```js
Number.range = function *range(start, end=undefined, step=1) {
  if (end === undefined) [start, end] = [0, start];
  if (end === undefined) end = Infinity;
  for (let i = 0; i < end; i += step) {
    yield i;
  }
};
```


On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just return a generator
or at least something that extends a generator, though. I wonder if it's viable to implement
something akin to `.length` on ranges, which could be natural numbers or `Infinity`.

As for numbers, I don't see any issues. One issue that came up in the original thread was
that string ranges may need a better definition, as ["A".."C"] might not necessarily transpile
to be a generator that yields "A", "B" and "C".

On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> normal method call equivalent to `(1).to(10)`, with optional parentheses
> removed.
>
> Also, I'd prefer this to be a generator instead, so infinite ranges are
> also possible, and so it doesn't have to be eager.
>
> On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> wrote:
>
> > How about this
> >
> > ```
> > for ( i of Array.range(1, 10) ) { ... }
> > // OR
> > for ( i of [1..10] )  { ... }
> > ```
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Michael J. Ryan

If there's a Number.range, if suggest a corresponding String.range for character ranges...  Agreed on it being a utility function over me syntax.


On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]> wrote:

If string ranges are based on character codes, it will (for the Latin alphabet, at least, not necessarily for other languages).

I would prefer a function over syntax, though, since it would be more easily adopted (polyfill > syntax), and it would fit more idiomatically with the rest of the language (which also uses functions for most utilities).

Maybe a `Number.range` would work?

```js
Number.range = function *range(start, end=undefined, step=1) {
  if (end === undefined) [start, end] = [0, start];
  if (end === undefined) end = Infinity;
  for (let i = 0; i < end; i += step) {
    yield i;
  }
};
```


On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just return a generator
or at least something that extends a generator, though. I wonder if it's viable to implement
something akin to `.length` on ranges, which could be natural numbers or `Infinity`.

As for numbers, I don't see any issues. One issue that came up in the original thread was
that string ranges may need a better definition, as ["A".."C"] might not necessarily transpile
to be a generator that yields "A", "B" and "C".

On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> normal method call equivalent to `(1).to(10)`, with optional parentheses
> removed.
>
> Also, I'd prefer this to be a generator instead, so infinite ranges are
> also possible, and so it doesn't have to be eager.
>
> On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> wrote:
>
> > How about this
> >
> > ```
> > for ( i of Array.range(1, 10) ) { ... }
> > // OR
> > for ( i of [1..10] )  { ... }
> > ```
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Viktor Kronvall
For `String.range` what would the expected result of `String.range('A','zzz')` be?

Is an exhaustive pattern expected?

`['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:

If there's a Number.range, if suggest a corresponding String.range for character ranges...  Agreed on it being a utility function over me syntax.


On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]> wrote:

If string ranges are based on character codes, it will (for the Latin alphabet, at least, not necessarily for other languages).

I would prefer a function over syntax, though, since it would be more easily adopted (polyfill > syntax), and it would fit more idiomatically with the rest of the language (which also uses functions for most utilities).

Maybe a `Number.range` would work?

```js
Number.range = function *range(start, end=undefined, step=1) {
  if (end === undefined) [start, end] = [0, start];
  if (end === undefined) end = Infinity;
  for (let i = 0; i < end; i += step) {
    yield i;
  }
};
```


On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just return a generator
or at least something that extends a generator, though. I wonder if it's viable to implement
something akin to `.length` on ranges, which could be natural numbers or `Infinity`.

As for numbers, I don't see any issues. One issue that came up in the original thread was
that string ranges may need a better definition, as ["A".."C"] might not necessarily transpile
to be a generator that yields "A", "B" and "C".

On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> normal method call equivalent to `(1).to(10)`, with optional parentheses
> removed.
>
> Also, I'd prefer this to be a generator instead, so infinite ranges are
> also possible, and so it doesn't have to be eager.
>
> On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> wrote:
>
> > How about this
> >
> > ```
> > for ( i of Array.range(1, 10) ) { ... }
> > // OR
> > for ( i of [1..10] )  { ... }
> > ```
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

kdex
About the code points: `String.range` should also handle surrogate pairs, similar to for..of does it.
About `String.range("A", "zzz")`: Do any other possibilities even make sense?

On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:

> For `String.range` what would the expected result of
> `String.range('A','zzz')` be?
>
> Is an exhaustive pattern expected?
>
> `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> 2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:
>
> > If there's a Number.range, if suggest a corresponding String.range for
> > character ranges...  Agreed on it being a utility function over me syntax.
> >
> > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]> wrote:
> >
> > If string ranges are based on character codes, it will (for the Latin
> > alphabet, at least, not necessarily for other languages).
> >
> > I would prefer a function over syntax, though, since it would be more
> > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > with the rest of the language (which also uses functions for most
> > utilities).
> >
> > Maybe a `Number.range` would work?
> >
> > ```js
> > Number.range = function *range(start, end=undefined, step=1) {
> >   if (end === undefined) [start, end] = [0, start];
> >   if (end === undefined) end = Infinity;
> >   for (let i = 0; i < end; i += step) {
> >     yield i;
> >   }
> > };
> > ```
> >
> > On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
> >
> > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > return a generator
> > or at least something that extends a generator, though. I wonder if it's
> > viable to implement
> > something akin to `.length` on ranges, which could be natural numbers or
> > `Infinity`.
> >
> > As for numbers, I don't see any issues. One issue that came up in the
> > original thread was
> > that string ranges may need a better definition, as ["A".."C"] might not
> > necessarily transpile
> > to be a generator that yields "A", "B" and "C".
> >
> > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> > > normal method call equivalent to `(1).to(10)`, with optional parentheses
> > > removed.
> > >
> > > Also, I'd prefer this to be a generator instead, so infinite ranges are
> > > also possible, and so it doesn't have to be eager.
> > >
> > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> > > wrote:
> > >
> > > > How about this
> > > >
> > > > ```
> > > > for ( i of Array.range(1, 10) ) { ... }
> > > > // OR
> > > > for ( i of [1..10] )  { ... }
> > > > ```
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Viktor Kronvall
Actually, after giving it some more thought for that case there is just that one possibility that makes sense.

However, there are more ambiguous cases such as `String.range("AAA", "ZZZ")` (should all letters increase at once or should the rightmost letter be incremented first)

Also, how would range handle the arguments in inverted order? Should there be a decreasing range or should it terminate with no elements in the iterator?
2016年11月3日(木) 21:05 kdex <[hidden email]>:
About the code points: `String.range` should also handle surrogate pairs, similar to for..of does it.
About `String.range("A", "zzz")`: Do any other possibilities even make sense?

On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:
> For `String.range` what would the expected result of
> `String.range('A','zzz')` be?
>
> Is an exhaustive pattern expected?
>
> `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> 2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:
>
> > If there's a Number.range, if suggest a corresponding String.range for
> > character ranges...  Agreed on it being a utility function over me syntax.
> >
> > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]> wrote:
> >
> > If string ranges are based on character codes, it will (for the Latin
> > alphabet, at least, not necessarily for other languages).
> >
> > I would prefer a function over syntax, though, since it would be more
> > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > with the rest of the language (which also uses functions for most
> > utilities).
> >
> > Maybe a `Number.range` would work?
> >
> > ```js
> > Number.range = function *range(start, end=undefined, step=1) {
> >   if (end === undefined) [start, end] = [0, start];
> >   if (end === undefined) end = Infinity;
> >   for (let i = 0; i < end; i += step) {
> >     yield i;
> >   }
> > };
> > ```
> >
> > On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
> >
> > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > return a generator
> > or at least something that extends a generator, though. I wonder if it's
> > viable to implement
> > something akin to `.length` on ranges, which could be natural numbers or
> > `Infinity`.
> >
> > As for numbers, I don't see any issues. One issue that came up in the
> > original thread was
> > that string ranges may need a better definition, as ["A".."C"] might not
> > necessarily transpile
> > to be a generator that yields "A", "B" and "C".
> >
> > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> > > normal method call equivalent to `(1).to(10)`, with optional parentheses
> > > removed.
> > >
> > > Also, I'd prefer this to be a generator instead, so infinite ranges are
> > > also possible, and so it doesn't have to be eager.
> > >
> > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> > > wrote:
> > >
> > > > How about this
> > > >
> > > > ```
> > > > for ( i of Array.range(1, 10) ) { ... }
> > > > // OR
> > > > for ( i of [1..10] )  { ... }
> > > > ```
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Viktor Kronvall
Even more interestingly what would `String.range("","zzz")` produce. From what code point is the range started? Will this throw? Is the empty string included in the iterator?
2016年11月3日(木) 21:18 Viktor Kronvall <[hidden email]>:
Actually, after giving it some more thought for that case there is just that one possibility that makes sense.

However, there are more ambiguous cases such as `String.range("AAA", "ZZZ")` (should all letters increase at once or should the rightmost letter be incremented first)

Also, how would range handle the arguments in inverted order? Should there be a decreasing range or should it terminate with no elements in the iterator?
2016年11月3日(木) 21:05 kdex <[hidden email]>:
About the code points: `String.range` should also handle surrogate pairs, similar to for..of does it.
About `String.range("A", "zzz")`: Do any other possibilities even make sense?

On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:
> For `String.range` what would the expected result of
> `String.range('A','zzz')` be?
>
> Is an exhaustive pattern expected?
>
> `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> 2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:
>
> > If there's a Number.range, if suggest a corresponding String.range for
> > character ranges...  Agreed on it being a utility function over me syntax.
> >
> > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]> wrote:
> >
> > If string ranges are based on character codes, it will (for the Latin
> > alphabet, at least, not necessarily for other languages).
> >
> > I would prefer a function over syntax, though, since it would be more
> > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > with the rest of the language (which also uses functions for most
> > utilities).
> >
> > Maybe a `Number.range` would work?
> >
> > ```js
> > Number.range = function *range(start, end=undefined, step=1) {
> >   if (end === undefined) [start, end] = [0, start];
> >   if (end === undefined) end = Infinity;
> >   for (let i = 0; i < end; i += step) {
> >     yield i;
> >   }
> > };
> > ```
> >
> > On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
> >
> > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > return a generator
> > or at least something that extends a generator, though. I wonder if it's
> > viable to implement
> > something akin to `.length` on ranges, which could be natural numbers or
> > `Infinity`.
> >
> > As for numbers, I don't see any issues. One issue that came up in the
> > original thread was
> > that string ranges may need a better definition, as ["A".."C"] might not
> > necessarily transpile
> > to be a generator that yields "A", "B" and "C".
> >
> > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> > > normal method call equivalent to `(1).to(10)`, with optional parentheses
> > > removed.
> > >
> > > Also, I'd prefer this to be a generator instead, so infinite ranges are
> > > also possible, and so it doesn't have to be eager.
> > >
> > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> > > wrote:
> > >
> > > > How about this
> > > >
> > > > ```
> > > > for ( i of Array.range(1, 10) ) { ... }
> > > > // OR
> > > > for ( i of [1..10] )  { ... }
> > > > ```
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Isiah Meadows-2

Could we just *not* have a `String.range`?

1. Needing a list of alphabetically sorted characters is a bit niche, even in the Latin alphabet.
2. It's ambiguous whether it should include symbols or not. (think: `String.range("A", "z")`)
3. If it's Unicode-aware, how should it act with, say, `String.range("A", "π")`?


On Thu, Nov 3, 2016, 16:21 Viktor Kronvall <[hidden email]> wrote:
Even more interestingly what would `String.range("","zzz")` produce. From what code point is the range started? Will this throw? Is the empty string included in the iterator?
2016年11月3日(木) 21:18 Viktor Kronvall <[hidden email]>:
Actually, after giving it some more thought for that case there is just that one possibility that makes sense.

However, there are more ambiguous cases such as `String.range("AAA", "ZZZ")` (should all letters increase at once or should the rightmost letter be incremented first)

Also, how would range handle the arguments in inverted order? Should there be a decreasing range or should it terminate with no elements in the iterator?
2016年11月3日(木) 21:05 kdex <[hidden email]>:
About the code points: `String.range` should also handle surrogate pairs, similar to for..of does it.
About `String.range("A", "zzz")`: Do any other possibilities even make sense?

On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:
> For `String.range` what would the expected result of
> `String.range('A','zzz')` be?
>
> Is an exhaustive pattern expected?
>
> `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> 2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:
>
> > If there's a Number.range, if suggest a corresponding String.range for
> > character ranges...  Agreed on it being a utility function over me syntax.
> >
> > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]> wrote:
> >
> > If string ranges are based on character codes, it will (for the Latin
> > alphabet, at least, not necessarily for other languages).
> >
> > I would prefer a function over syntax, though, since it would be more
> > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > with the rest of the language (which also uses functions for most
> > utilities).
> >
> > Maybe a `Number.range` would work?
> >
> > ```js
> > Number.range = function *range(start, end=undefined, step=1) {
> >   if (end === undefined) [start, end] = [0, start];
> >   if (end === undefined) end = Infinity;
> >   for (let i = 0; i < end; i += step) {
> >     yield i;
> >   }
> > };
> > ```
> >
> > On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
> >
> > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > return a generator
> > or at least something that extends a generator, though. I wonder if it's
> > viable to implement
> > something akin to `.length` on ranges, which could be natural numbers or
> > `Infinity`.
> >
> > As for numbers, I don't see any issues. One issue that came up in the
> > original thread was
> > that string ranges may need a better definition, as ["A".."C"] might not
> > necessarily transpile
> > to be a generator that yields "A", "B" and "C".
> >
> > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> > > normal method call equivalent to `(1).to(10)`, with optional parentheses
> > > removed.
> > >
> > > Also, I'd prefer this to be a generator instead, so infinite ranges are
> > > also possible, and so it doesn't have to be eager.
> > >
> > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> > > wrote:
> > >
> > > > How about this
> > > >
> > > > ```
> > > > for ( i of Array.range(1, 10) ) { ... }
> > > > // OR
> > > > for ( i of [1..10] )  { ... }
> > > > ```
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

kdex
I don't think we should give the Latin alphabet special treatment. This is solely about Unicode codepoints.

I'd expect…

        - `String.range("AAA", "ZZZ")` to generate ["AAA", "AAB", …, "ABA", … "ZZZ"].
        - `String.range("", "zzz")` to either throw or be equivalent to `String.range("\u0000", "zzz")`.
        - `String.range("z", "A")` to either throw or wrap around at "\u{ffffff}" and therefore include
                all codepoints.
        - `String.range("A", "z")` to include symbols, too. If you only need letters in latin-1, you should
                either write a custom latin-1 generator or skip these values with a RegExp when iterating
                over them. Or just use two ranges, one for A-Z and one for a-z.
        - `String.range("A", "π")` to include all characters from "\u0065" (A) to "\u03c0" (π).
        - `String.range("😀", "😂")` to generate ["😀", "😬", "😁", "😂"] (surrogate pairs being respected)

For more complicated cases, it might make more sense to make users pass a function to `String.range`
for fine-grained control, or maybe just make users create their own generators.

On Thursday, November 3, 2016 8:54:16 PM CET you wrote:

> Could we just *not* have a `String.range`?
>
> 1. Needing a list of alphabetically sorted characters is a bit niche, even
> in the Latin alphabet.
> 2. It's ambiguous whether it should include symbols or not. (think:
> `String.range("A", "z")`)
> 3. If it's Unicode-aware, how should it act with, say, `String.range("A",
> "π")`?
>
> On Thu, Nov 3, 2016, 16:21 Viktor Kronvall <[hidden email]>
> wrote:
>
> > Even more interestingly what would `String.range("","zzz")` produce. From
> > what code point is the range started? Will this throw? Is the empty string
> > included in the iterator?
> > 2016年11月3日(木) 21:18 Viktor Kronvall <[hidden email]>:
> >
> > Actually, after giving it some more thought for that case there is just
> > that one possibility that makes sense.
> >
> > However, there are more ambiguous cases such as `String.range("AAA",
> > "ZZZ")` (should all letters increase at once or should the rightmost letter
> > be incremented first)
> >
> > Also, how would range handle the arguments in inverted order? Should there
> > be a decreasing range or should it terminate with no elements in the
> > iterator?
> > 2016年11月3日(木) 21:05 kdex <[hidden email]>:
> >
> > About the code points: `String.range` should also handle surrogate pairs,
> > similar to for..of does it.
> > About `String.range("A", "zzz")`: Do any other possibilities even make
> > sense?
> >
> > On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:
> > > For `String.range` what would the expected result of
> > > `String.range('A','zzz')` be?
> > >
> > > Is an exhaustive pattern expected?
> > >
> > > `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> > > 2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:
> > >
> > > > If there's a Number.range, if suggest a corresponding String.range for
> > > > character ranges...  Agreed on it being a utility function over me
> > syntax.
> > > >
> > > > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]>
> > wrote:
> > > >
> > > > If string ranges are based on character codes, it will (for the Latin
> > > > alphabet, at least, not necessarily for other languages).
> > > >
> > > > I would prefer a function over syntax, though, since it would be more
> > > > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > > > with the rest of the language (which also uses functions for most
> > > > utilities).
> > > >
> > > > Maybe a `Number.range` would work?
> > > >
> > > > ```js
> > > > Number.range = function *range(start, end=undefined, step=1) {
> > > >   if (end === undefined) [start, end] = [0, start];
> > > >   if (end === undefined) end = Infinity;
> > > >   for (let i = 0; i < end; i += step) {
> > > >     yield i;
> > > >   }
> > > > };
> > > > ```
> > > >
> > > > On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
> > > >
> > > > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > > > return a generator
> > > > or at least something that extends a generator, though. I wonder if
> > it's
> > > > viable to implement
> > > > something akin to `.length` on ranges, which could be natural numbers
> > or
> > > > `Infinity`.
> > > >
> > > > As for numbers, I don't see any issues. One issue that came up in the
> > > > original thread was
> > > > that string ranges may need a better definition, as ["A".."C"] might
> > not
> > > > necessarily transpile
> > > > to be a generator that yields "A", "B" and "C".
> > > >
> > > > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > > > I'll note, just for clarity, that Scala's `1 to 10` is technically
> > just a
> > > > > normal method call equivalent to `(1).to(10)`, with optional
> > parentheses
> > > > > removed.
> > > > >
> > > > > Also, I'd prefer this to be a generator instead, so infinite ranges
> > are
> > > > > also possible, and so it doesn't have to be eager.
> > > > >
> > > > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <
> > [hidden email]>
> > > > > wrote:
> > > > >
> > > > > > How about this
> > > > > >
> > > > > > ```
> > > > > > for ( i of Array.range(1, 10) ) { ... }
> > > > > > // OR
> > > > > > for ( i of [1..10] )  { ... }
> > > > > > ```
> > > > > >
> > > > > >
> > > > > > _______________________________________________
> > > > > > es-discuss mailing list
> > > > > > [hidden email]
> > > > > > https://mail.mozilla.org/listinfo/es-discuss
> > > > > >
> > > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Zach Lym
With regard to syntax, Rust's is the best that I've seen: '0 ... 10' inclusive and  '1 .. 9' exclusive.

for ( i of [1..10] )  { ... }

Why do we need to clutter the syntax with brackets?

Strings are something that's needed, I've been tinkering with a LINQ syntax for IndexedDB and a Range type would make it easier to take advantage of indexing.

However, there is not right answer here: is "10A" > "001B"?  What if I'm storing DNS labels, where `mail.example.com` < `*.example.com`?  

I think there should be a sane default (perhaps with syntactic sugar to specify region encoding and Unicode normalization) but just people to create extend a Range object so they can build their own semantics.

Thank you,
-Zach Lym

On Thu, Nov 3, 2016 at 2:26 PM, kdex <[hidden email]> wrote:
I don't think we should give the Latin alphabet special treatment. This is solely about Unicode codepoints.

I'd expect…

        - `String.range("AAA", "ZZZ")` to generate ["AAA", "AAB", …, "ABA", … "ZZZ"].
        - `String.range("", "zzz")` to either throw or be equivalent to `String.range("\u0000", "zzz")`.
        - `String.range("z", "A")` to either throw or wrap around at "\u{ffffff}" and therefore include
                all codepoints.
        - `String.range("A", "z")` to include symbols, too. If you only need letters in latin-1, you should
                either write a custom latin-1 generator or skip these values with a RegExp when iterating
                over them. Or just use two ranges, one for A-Z and one for a-z.
        - `String.range("A", "π")` to include all characters from "\u0065" (A) to "\u03c0" (π).
        - `String.range("😀", "😂")` to generate ["😀", "😬", "😁", "😂"] (surrogate pairs being respected)

For more complicated cases, it might make more sense to make users pass a function to `String.range`
for fine-grained control, or maybe just make users create their own generators.

On Thursday, November 3, 2016 8:54:16 PM CET you wrote:
> Could we just *not* have a `String.range`?
>
> 1. Needing a list of alphabetically sorted characters is a bit niche, even
> in the Latin alphabet.
> 2. It's ambiguous whether it should include symbols or not. (think:
> `String.range("A", "z")`)
> 3. If it's Unicode-aware, how should it act with, say, `String.range("A",
> "π")`?
>
> On Thu, Nov 3, 2016, 16:21 Viktor Kronvall <[hidden email]>
> wrote:
>
> > Even more interestingly what would `String.range("","zzz")` produce. From
> > what code point is the range started? Will this throw? Is the empty string
> > included in the iterator?
> > 2016年11月3日(木) 21:18 Viktor Kronvall <[hidden email]>:
> >
> > Actually, after giving it some more thought for that case there is just
> > that one possibility that makes sense.
> >
> > However, there are more ambiguous cases such as `String.range("AAA",
> > "ZZZ")` (should all letters increase at once or should the rightmost letter
> > be incremented first)
> >
> > Also, how would range handle the arguments in inverted order? Should there
> > be a decreasing range or should it terminate with no elements in the
> > iterator?
> > 2016年11月3日(木) 21:05 kdex <[hidden email]>:
> >
> > About the code points: `String.range` should also handle surrogate pairs,
> > similar to for..of does it.
> > About `String.range("A", "zzz")`: Do any other possibilities even make
> > sense?
> >
> > On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:
> > > For `String.range` what would the expected result of
> > > `String.range('A','zzz')` be?
> > >
> > > Is an exhaustive pattern expected?
> > >
> > > `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> > > 2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:
> > >
> > > > If there's a Number.range, if suggest a corresponding String.range for
> > > > character ranges...  Agreed on it being a utility function over me
> > syntax.
> > > >
> > > > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]>
> > wrote:
> > > >
> > > > If string ranges are based on character codes, it will (for the Latin
> > > > alphabet, at least, not necessarily for other languages).
> > > >
> > > > I would prefer a function over syntax, though, since it would be more
> > > > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > > > with the rest of the language (which also uses functions for most
> > > > utilities).
> > > >
> > > > Maybe a `Number.range` would work?
> > > >
> > > > ```js
> > > > Number.range = function *range(start, end=undefined, step=1) {
> > > >   if (end === undefined) [start, end] = [0, start];
> > > >   if (end === undefined) end = Infinity;
> > > >   for (let i = 0; i < end; i += step) {
> > > >     yield i;
> > > >   }
> > > > };
> > > > ```
> > > >
> > > > On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
> > > >
> > > > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > > > return a generator
> > > > or at least something that extends a generator, though. I wonder if
> > it's
> > > > viable to implement
> > > > something akin to `.length` on ranges, which could be natural numbers
> > or
> > > > `Infinity`.
> > > >
> > > > As for numbers, I don't see any issues. One issue that came up in the
> > > > original thread was
> > > > that string ranges may need a better definition, as ["A".."C"] might
> > not
> > > > necessarily transpile
> > > > to be a generator that yields "A", "B" and "C".
> > > >
> > > > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > > > I'll note, just for clarity, that Scala's `1 to 10` is technically
> > just a
> > > > > normal method call equivalent to `(1).to(10)`, with optional
> > parentheses
> > > > > removed.
> > > > >
> > > > > Also, I'd prefer this to be a generator instead, so infinite ranges
> > are
> > > > > also possible, and so it doesn't have to be eager.
> > > > >
> > > > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <
> > [hidden email]>
> > > > > wrote:
> > > > >
> > > > > > How about this
> > > > > >
> > > > > > ```
> > > > > > for ( i of Array.range(1, 10) ) { ... }
> > > > > > // OR
> > > > > > for ( i of [1..10] )  { ... }
> > > > > > ```
> > > > > >
> > > > > >
> > > > > > _______________________________________________
> > > > > > es-discuss mailing list
> > > > > > [hidden email]
> > > > > > https://mail.mozilla.org/listinfo/es-discuss
> > > > > >
> > > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Alexander Jones
In reply to this post by Isiah Meadows-2
+1000

Classic feature creep with basically zero application.

On Thursday, 3 November 2016, Isiah Meadows <[hidden email]> wrote:

Could we just *not* have a `String.range`?

1. Needing a list of alphabetically sorted characters is a bit niche, even in the Latin alphabet.
2. It's ambiguous whether it should include symbols or not. (think: `String.range("A", "z")`)
3. If it's Unicode-aware, how should it act with, say, `String.range("A", "π")`?


On Thu, Nov 3, 2016, 16:21 Viktor Kronvall <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;viktor.kronvall@gmail.com&#39;);" target="_blank">viktor.kronvall@...> wrote:
Even more interestingly what would `String.range("","zzz")` produce. From what code point is the range started? Will this throw? Is the empty string included in the iterator?
2016年11月3日(木) 21:18 Viktor Kronvall <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;viktor.kronvall@gmail.com&#39;);" target="_blank">viktor.kronvall@...>:
Actually, after giving it some more thought for that case there is just that one possibility that makes sense.

However, there are more ambiguous cases such as `String.range("AAA", "ZZZ")` (should all letters increase at once or should the rightmost letter be incremented first)

Also, how would range handle the arguments in inverted order? Should there be a decreasing range or should it terminate with no elements in the iterator?
2016年11月3日(木) 21:05 kdex <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;kdex@kdex.de&#39;);" target="_blank">kdex@...>:
About the code points: `String.range` should also handle surrogate pairs, similar to for..of does it.
About `String.range("A", "zzz")`: Do any other possibilities even make sense?

On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:
> For `String.range` what would the expected result of
> `String.range('A','zzz')` be?
>
> Is an exhaustive pattern expected?
>
> `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> 2016年11月3日(木) 19:21 Michael J. Ryan <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;tracker1@gmail.com&#39;);" target="_blank">tracker1@...>:
>
> > If there's a Number.range, if suggest a corresponding String.range for
> > character ranges...  Agreed on it being a utility function over me syntax.
> >
> > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;isiahmeadows@gmail.com&#39;);" target="_blank">isiahmeadows@...> wrote:
> >
> > If string ranges are based on character codes, it will (for the Latin
> > alphabet, at least, not necessarily for other languages).
> >
> > I would prefer a function over syntax, though, since it would be more
> > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > with the rest of the language (which also uses functions for most
> > utilities).
> >
> > Maybe a `Number.range` would work?
> >
> > ```js
> > Number.range = function *range(start, end=undefined, step=1) {
> >   if (end === undefined) [start, end] = [0, start];
> >   if (end === undefined) end = Infinity;
> >   for (let i = 0; i < end; i += step) {
> >     yield i;
> >   }
> > };
> > ```
> >
> > On Thu, Nov 3, 2016, 12:56 kdex <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;kdex@kdex.de&#39;);" target="_blank">kdex@...> wrote:
> >
> > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > return a generator
> > or at least something that extends a generator, though. I wonder if it's
> > viable to implement
> > something akin to `.length` on ranges, which could be natural numbers or
> > `Infinity`.
> >
> > As for numbers, I don't see any issues. One issue that came up in the
> > original thread was
> > that string ranges may need a better definition, as ["A".."C"] might not
> > necessarily transpile
> > to be a generator that yields "A", "B" and "C".
> >
> > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> > > normal method call equivalent to `(1).to(10)`, with optional parentheses
> > > removed.
> > >
> > > Also, I'd prefer this to be a generator instead, so infinite ranges are
> > > also possible, and so it doesn't have to be eager.
> > >
> > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;oao.hikaru.oao@gmail.com&#39;);" target="_blank">oao.hikaru.oao@...>
> > > wrote:
> > >
> > > > How about this
> > > >
> > > > ```
> > > > for ( i of Array.range(1, 10) ) { ... }
> > > > // OR
> > > > for ( i of [1..10] )  { ... }
> > > > ```
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;es-discuss@mozilla.org&#39;);" target="_blank">es-discuss@...
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > >
> > _______________________________________________
> > es-discuss mailing list
> > <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;es-discuss@mozilla.org&#39;);" target="_blank">es-discuss@...
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;es-discuss@mozilla.org&#39;);" target="_blank">es-discuss@...
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;es-discuss@mozilla.org&#39;);" target="_blank">es-discuss@...
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;es-discuss@mozilla.org&#39;);" target="_blank">es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;es-discuss@mozilla.org&#39;);" target="_blank">es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Michael J. Ryan

Your right... The more I think about it... 

    Number.range(from=0, to=Number. MAX_SAFE_INTEGER)

Is probably best, as a signature, then using .map(...) If you want a string representation... 

For that matter, wouldn't mind seeing more of the array utility methods, like map, added to strings, iterating through each character, same as .split('').map, but that's another discussion.


On Nov 4, 2016 3:11 AM, "Alexander Jones" <[hidden email]> wrote:
+1000

Classic feature creep with basically zero application.


On Thursday, 3 November 2016, Isiah Meadows <[hidden email]> wrote:

Could we just *not* have a `String.range`?

1. Needing a list of alphabetically sorted characters is a bit niche, even in the Latin alphabet.
2. It's ambiguous whether it should include symbols or not. (think: `String.range("A", "z")`)
3. If it's Unicode-aware, how should it act with, say, `String.range("A", "π")`?


On Thu, Nov 3, 2016, 16:21 Viktor Kronvall <[hidden email]> wrote:
Even more interestingly what would `String.range("","zzz")` produce. From what code point is the range started? Will this throw? Is the empty string included in the iterator?
2016年11月3日(木) 21:18 Viktor Kronvall <[hidden email]>:
Actually, after giving it some more thought for that case there is just that one possibility that makes sense.

However, there are more ambiguous cases such as `String.range("AAA", "ZZZ")` (should all letters increase at once or should the rightmost letter be incremented first)

Also, how would range handle the arguments in inverted order? Should there be a decreasing range or should it terminate with no elements in the iterator?
2016年11月3日(木) 21:05 kdex <[hidden email]>:
About the code points: `String.range` should also handle surrogate pairs, similar to for..of does it.
About `String.range("A", "zzz")`: Do any other possibilities even make sense?

On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:
> For `String.range` what would the expected result of
> `String.range('A','zzz')` be?
>
> Is an exhaustive pattern expected?
>
> `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> 2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:
>
> > If there's a Number.range, if suggest a corresponding String.range for
> > character ranges...  Agreed on it being a utility function over me syntax.
> >
> > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]> wrote:
> >
> > If string ranges are based on character codes, it will (for the Latin
> > alphabet, at least, not necessarily for other languages).
> >
> > I would prefer a function over syntax, though, since it would be more
> > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > with the rest of the language (which also uses functions for most
> > utilities).
> >
> > Maybe a `Number.range` would work?
> >
> > ```js
> > Number.range = function *range(start, end=undefined, step=1) {
> >   if (end === undefined) [start, end] = [0, start];
> >   if (end === undefined) end = Infinity;
> >   for (let i = 0; i < end; i += step) {
> >     yield i;
> >   }
> > };
> > ```
> >
> > On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
> >
> > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > return a generator
> > or at least something that extends a generator, though. I wonder if it's
> > viable to implement
> > something akin to `.length` on ranges, which could be natural numbers or
> > `Infinity`.
> >
> > As for numbers, I don't see any issues. One issue that came up in the
> > original thread was
> > that string ranges may need a better definition, as ["A".."C"] might not
> > necessarily transpile
> > to be a generator that yields "A", "B" and "C".
> >
> > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> > > normal method call equivalent to `(1).to(10)`, with optional parentheses
> > > removed.
> > >
> > > Also, I'd prefer this to be a generator instead, so infinite ranges are
> > > also possible, and so it doesn't have to be eager.
> > >
> > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> > > wrote:
> > >
> > > > How about this
> > > >
> > > > ```
> > > > for ( i of Array.range(1, 10) ) { ... }
> > > > // OR
> > > > for ( i of [1..10] )  { ... }
> > > > ```
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Jordan Harband
Here you go:

1) `function* range(start, end) { for (const i = +start; i < end; ++i) { yield i; } }`
2) `function range(start, end) { return Array.from({ length: start - end }, (_, i) => start + i); }`

On Fri, Nov 4, 2016 at 7:42 AM, Michael J. Ryan <[hidden email]> wrote:

Your right... The more I think about it... 

    Number.range(from=0, to=Number. MAX_SAFE_INTEGER)

Is probably best, as a signature, then using .map(...) If you want a string representation... 

For that matter, wouldn't mind seeing more of the array utility methods, like map, added to strings, iterating through each character, same as .split('').map, but that's another discussion.


On Nov 4, 2016 3:11 AM, "Alexander Jones" <[hidden email]> wrote:
+1000

Classic feature creep with basically zero application.


On Thursday, 3 November 2016, Isiah Meadows <[hidden email]> wrote:

Could we just *not* have a `String.range`?

1. Needing a list of alphabetically sorted characters is a bit niche, even in the Latin alphabet.
2. It's ambiguous whether it should include symbols or not. (think: `String.range("A", "z")`)
3. If it's Unicode-aware, how should it act with, say, `String.range("A", "π")`?


On Thu, Nov 3, 2016, 16:21 Viktor Kronvall <[hidden email]> wrote:
Even more interestingly what would `String.range("","zzz")` produce. From what code point is the range started? Will this throw? Is the empty string included in the iterator?
2016年11月3日(木) 21:18 Viktor Kronvall <[hidden email]>:
Actually, after giving it some more thought for that case there is just that one possibility that makes sense.

However, there are more ambiguous cases such as `String.range("AAA", "ZZZ")` (should all letters increase at once or should the rightmost letter be incremented first)

Also, how would range handle the arguments in inverted order? Should there be a decreasing range or should it terminate with no elements in the iterator?
2016年11月3日(木) 21:05 kdex <[hidden email]>:
About the code points: `String.range` should also handle surrogate pairs, similar to for..of does it.
About `String.range("A", "zzz")`: Do any other possibilities even make sense?

On Thursday, November 3, 2016 6:47:04 PM CET Viktor Kronvall wrote:
> For `String.range` what would the expected result of
> `String.range('A','zzz')` be?
>
> Is an exhaustive pattern expected?
>
> `['A','B','C',...'Z','a','b','c',...,'z','AA','AB',...]`
> 2016年11月3日(木) 19:21 Michael J. Ryan <[hidden email]>:
>
> > If there's a Number.range, if suggest a corresponding String.range for
> > character ranges...  Agreed on it being a utility function over me syntax.
> >
> > On Nov 3, 2016 10:25 AM, "Isiah Meadows" <[hidden email]> wrote:
> >
> > If string ranges are based on character codes, it will (for the Latin
> > alphabet, at least, not necessarily for other languages).
> >
> > I would prefer a function over syntax, though, since it would be more
> > easily adopted (polyfill > syntax), and it would fit more idiomatically
> > with the rest of the language (which also uses functions for most
> > utilities).
> >
> > Maybe a `Number.range` would work?
> >
> > ```js
> > Number.range = function *range(start, end=undefined, step=1) {
> >   if (end === undefined) [start, end] = [0, start];
> >   if (end === undefined) end = Infinity;
> >   for (let i = 0; i < end; i += step) {
> >     yield i;
> >   }
> > };
> > ```
> >
> > On Thu, Nov 3, 2016, 12:56 kdex <[hidden email]> wrote:
> >
> > Agreed. There's no reason why `Array.range` or `[1..10]` couldn't just
> > return a generator
> > or at least something that extends a generator, though. I wonder if it's
> > viable to implement
> > something akin to `.length` on ranges, which could be natural numbers or
> > `Infinity`.
> >
> > As for numbers, I don't see any issues. One issue that came up in the
> > original thread was
> > that string ranges may need a better definition, as ["A".."C"] might not
> > necessarily transpile
> > to be a generator that yields "A", "B" and "C".
> >
> > On Thursday, November 3, 2016 4:46:03 PM CET Isiah Meadows wrote:
> > > I'll note, just for clarity, that Scala's `1 to 10` is technically just a
> > > normal method call equivalent to `(1).to(10)`, with optional parentheses
> > > removed.
> > >
> > > Also, I'd prefer this to be a generator instead, so infinite ranges are
> > > also possible, and so it doesn't have to be eager.
> > >
> > > On Thu, Nov 3, 2016, 11:52 Hikaru Nakashima <[hidden email]>
> > > wrote:
> > >
> > > > How about this
> > > >
> > > > ```
> > > > for ( i of Array.range(1, 10) ) { ... }
> > > > // OR
> > > > for ( i of [1..10] )  { ... }
> > > > ```
> > > >
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> > > >
> > >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss
> >

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Mathias Bynens-2
On Fri, Nov 4, 2016 at 6:24 PM, Jordan Harband <[hidden email]> wrote:
> Here you go:
>
> 1) `function* range(start, end) { for (const i = +start; i < end; ++i) {
> yield i; } }`

For future reference: `++i` throws when `i` is a `const` binding. The
intended example uses `let` instead.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Andrea Giammarchi-2
FWIW: got bitten from same thing too ... the fact `for(const x of ...)` works would always mislead me to think a `for(const i; ... i++)` would too, even if it's completely different meaning/issue/matter.

Oh well

On Fri, Nov 4, 2016 at 5:34 PM, Mathias Bynens <[hidden email]> wrote:
On Fri, Nov 4, 2016 at 6:24 PM, Jordan Harband <[hidden email]> wrote:
> Here you go:
>
> 1) `function* range(start, end) { for (const i = +start; i < end; ++i) {
> yield i; } }`

For future reference: `++i` throws when `i` is a `const` binding. The
intended example uses `let` instead.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Jordan Harband
lol thanks, good call. too much habit typing "const" :-p s/const/let there

On Fri, Nov 4, 2016 at 10:39 AM, Andrea Giammarchi <[hidden email]> wrote:
FWIW: got bitten from same thing too ... the fact `for(const x of ...)` works would always mislead me to think a `for(const i; ... i++)` would too, even if it's completely different meaning/issue/matter.

Oh well

On Fri, Nov 4, 2016 at 5:34 PM, Mathias Bynens <[hidden email]> wrote:
On Fri, Nov 4, 2016 at 6:24 PM, Jordan Harband <[hidden email]> wrote:
> Here you go:
>
> 1) `function* range(start, end) { for (const i = +start; i < end; ++i) {
> yield i; } }`

For future reference: `++i` throws when `i` is a `const` binding. The
intended example uses `let` instead.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Ranges

Andy Earnshaw-2
In reply to this post by Zach Lym
On Thu, 3 Nov 2016 at 22:11 Zach Lym <[hidden email]> wrote:
With regard to syntax, Rust's is the best that I've seen: '0 ... 10' inclusive and  '1 .. 9' exclusive.

I love Rust's syntax too, but unfortunately 1..<something> is already valid ES syntax:

    1..Infinity
    //-> undefined

    Number.prototype.foo = 'bar'
    1..foo
    //-> 'bar'

You'd have to enforce the spaces around the operator, which would be weird IMO.  `...` would be ok though.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
123