Javascript Language feature Idea

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

Javascript Language feature Idea

RacerD123
In other programming languages such as python, you can specify the last element of a list with "list[-1]" but in javascript you have to do "list[list.length-1]". Is there maybe a way to make this feature in javascript?

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

Re: Javascript Language feature Idea

Sander Deryckere
Since Array inherits from Object, it can have any key, including the key "-1". So something like list[-1] would break compatibility as users can now already assign values to the index -1.

If you want a short way to access the last element, it should probably be a function in the Array prototype. Something like list.last().

Regards,
Sander

2016-01-22 18:53 GMT+01:00 RacerD123 <[hidden email]>:
In other programming languages such as python, you can specify the last element of a list with "list[-1]" but in javascript you have to do "list[list.length-1]". Is there maybe a way to make this feature in javascript?

_______________________________________________
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: Javascript Language feature Idea

Jeremy Martin
Don't forget that Array#slice() can accept negative indexes. E.g.:

> ['a', 'b', 'c', 'd'].slice(-1)
[ 'd' ]

Which is nearly as terse as any Array.prototype addition would be, and I think it's at least expressive enough to alleviate the need for one.

On Fri, Jan 22, 2016 at 12:59 PM, Sander Deryckere <[hidden email]> wrote:
Since Array inherits from Object, it can have any key, including the key "-1". So something like list[-1] would break compatibility as users can now already assign values to the index -1.

If you want a short way to access the last element, it should probably be a function in the Array prototype. Something like list.last().

Regards,
Sander

2016-01-22 18:53 GMT+01:00 RacerD123 <[hidden email]>:
In other programming languages such as python, you can specify the last element of a list with "list[-1]" but in javascript you have to do "list[list.length-1]". Is there maybe a way to make this feature in javascript?

_______________________________________________
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




--
Jeremy Martin
<a href="tel:661.312.3853" value="+16613123853" target="_blank">661.312.3853
@jmar777

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

Re: Javascript Language feature Idea

Dmitry Soshnikov
In reply to this post by RacerD123
On Fri, Jan 22, 2016 at 9:53 AM, RacerD123 <[hidden email]> wrote:
In other programming languages such as python, you can specify the last element of a list with "list[-1]" but in javascript you have to do "list[list.length-1]". Is there maybe a way to make this feature in javascript?


This could be a good addition, although `slice` may work as well. This has been discussed previously as well in the last 4-5 years. In addition with non-enumerable properties you can just monkey-patch `Array.prototype` with the `peek` or `last` method. I guess approach when some library forces some method to widely used, can be a good candidate for adding it to the spec (this how it was with `Function.prototype.bind`, e.g., although today monkey-patching is a bad practice of course).

In addition, you can monkey-patch just `-1`, and other indexes, e.g. a gist from 5 years old, when a similar topic was proposed here: https://gist.github.com/DmitrySoshnikov/984921

Dmitry 

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

Re: Javascript Language feature Idea

Bradley Meck
Personally I prefer a well known symbol for marking you are grabbing from the end of the list rather than this wrapping behavior like D ( https://dlang.org/spec/arrays.html#array-length ). That said I think `.last` is more reasonable than changing prototypes.

On Fri, Jan 22, 2016 at 1:17 PM, Dmitry Soshnikov <[hidden email]> wrote:
On Fri, Jan 22, 2016 at 9:53 AM, RacerD123 <[hidden email]> wrote:
In other programming languages such as python, you can specify the last element of a list with "list[-1]" but in javascript you have to do "list[list.length-1]". Is there maybe a way to make this feature in javascript?


This could be a good addition, although `slice` may work as well. This has been discussed previously as well in the last 4-5 years. In addition with non-enumerable properties you can just monkey-patch `Array.prototype` with the `peek` or `last` method. I guess approach when some library forces some method to widely used, can be a good candidate for adding it to the spec (this how it was with `Function.prototype.bind`, e.g., although today monkey-patching is a bad practice of course).

In addition, you can monkey-patch just `-1`, and other indexes, e.g. a gist from 5 years old, when a similar topic was proposed here: https://gist.github.com/DmitrySoshnikov/984921

Dmitry 

_______________________________________________
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: Re: Javascript Language feature Idea

Alican Çubukçuoğlu
In reply to this post by RacerD123
More cool stuff:
```javascript
const arr = [1, 2, 3];

// Easy push
arr[] = 4; // => arr[arr.length];

// Easy s(p)lice
arr[begin, end];,
arr[begin,]; // => arr[begin, arr.length];
arr[begin, end] = [1, 2, 3];
```

A terrible example (terrible because this should be done with WebGL shaders):
```javascript
const image = [ /* Umbagajillion of RGBA pixels */ ];

function manipulate(rgba) {
  rgba[0] += 10;
  rgba[1] += 10;
  rgba[2] += 10;
}

for (let i = 0; i < image.length / 4; i++) {
  const begin = i * 4;
  const end = begin + 4;

  /*
    In case easy s(p)lice doesn't actually Array.p.slice
    and just creates a limited view of the array
    without breaking reference
    (image[begin, end] === image[begin, end])
  */
  manipulate(image[begin, end]);

  /*
    In case easy s(p)lice does Array.p.slice
    and creates a new array
    (image[begin, end] !== image[begin, end])
  */
  const pixel = image[begin, end];
  manipulate(pixel);
  image[begin, end] = pixel;
}
```

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

Re: Javascript Language feature Idea

kdex
I really adore the slicing syntax. Just as a quick fix, though,
> arr[begin,]; // => arr[begin, arr.length];
should obviously return `arr[begin, arr.length - 1]`.

Honestly, I'm not too sure if the "easy push" syntax is particularly self-
explanatory. There might be better alternatives.

On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:

> More cool stuff:
> ```javascript
> const arr = [1, 2, 3];
>
> // Easy push
> arr[] = 4; // => arr[arr.length];
>
> // Easy s(p)lice
> arr[begin, end];,
> arr[begin,]; // => arr[begin, arr.length];
> arr[begin, end] = [1, 2, 3];
> ```
>
> A terrible example (terrible because this should be done with WebGL
> shaders):
> ```javascript
> const image = [ /* Umbagajillion of RGBA pixels */ ];
>
> function manipulate(rgba) {
>   rgba[0] += 10;
>   rgba[1] += 10;
>   rgba[2] += 10;
> }
>
> for (let i = 0; i < image.length / 4; i++) {
>   const begin = i * 4;
>   const end = begin + 4;
>
>   /*
>     In case easy s(p)lice doesn't actually Array.p.slice
>     and just creates a limited view of the array
>     without breaking reference
>     (image[begin, end] === image[begin, end])
>   */
>   manipulate(image[begin, end]);
>
>   /*
>     In case easy s(p)lice does Array.p.slice
>     and creates a new array
>     (image[begin, end] !== image[begin, end])
>   */
>   const pixel = image[begin, end];
>   manipulate(pixel);
>   image[begin, end] = pixel;
> }
> ```


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

Re: Javascript Language feature Idea

kdex
In reply to this post by Sander Deryckere
Standardizing unused array keys breaks compatibility about as much as
extending the prototype does, really. Users can already mangle with both.

The problems are a little more subtle, and yes, it would severely break
backwards compatibility. Consider this example (accessing an element based on
a computed index):

```js
let a = [1, 2, 3];
let occurrence = a.indexOf(4);
a[occurrence];
```

Currently, this should result in `undefined`. After your proposal, you could
introduce a potential bug into every program that accesses arrays using an
index computed with `Array.prototype.indexOf`, since it would now return `3`.

On Freitag, 22. Januar 2016 18:59:06 CET Sander Deryckere wrote:

> Since Array inherits from Object, it can have any key, including the key
> "-1". So something like list[-1] would break compatibility as users can now
> already assign values to the index -1.
>
> If you want a short way to access the last element, it should probably be a
> function in the Array prototype. Something like list.last().
>
> Regards,
> Sander
>
> 2016-01-22 18:53 GMT+01:00 RacerD123 <[hidden email]>:
> > In other programming languages such as python, you can specify the last
> > element of a list with "list[-1]" but in javascript you have to do
> > "list[list.length-1]". Is there maybe a way to make this feature in
> > javascript?
> >
> > _______________________________________________
> > 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: Javascript Language feature Idea

Benoit Marchant
In reply to this post by kdex
Interesting! More pedestrian, it would be nice to finally have a version of splice that doesn't create a new other Array as a side effect.

> On Jan 22, 2016, at 13:00, kdex <[hidden email]> wrote:
>
> I really adore the slicing syntax. Just as a quick fix, though,
>> arr[begin,]; // => arr[begin, arr.length];
> should obviously return `arr[begin, arr.length - 1]`.
>
> Honestly, I'm not too sure if the "easy push" syntax is particularly self-
> explanatory. There might be better alternatives.
>
>> On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
>> More cool stuff:
>> ```javascript
>> const arr = [1, 2, 3];
>>
>> // Easy push
>> arr[] = 4; // => arr[arr.length];
>>
>> // Easy s(p)lice
>> arr[begin, end];,
>> arr[begin,]; // => arr[begin, arr.length];
>> arr[begin, end] = [1, 2, 3];
>> ```
>>
>> A terrible example (terrible because this should be done with WebGL
>> shaders):
>> ```javascript
>> const image = [ /* Umbagajillion of RGBA pixels */ ];
>>
>> function manipulate(rgba) {
>>  rgba[0] += 10;
>>  rgba[1] += 10;
>>  rgba[2] += 10;
>> }
>>
>> for (let i = 0; i < image.length / 4; i++) {
>>  const begin = i * 4;
>>  const end = begin + 4;
>>
>>  /*
>>    In case easy s(p)lice doesn't actually Array.p.slice
>>    and just creates a limited view of the array
>>    without breaking reference
>>    (image[begin, end] === image[begin, end])
>>  */
>>  manipulate(image[begin, end]);
>>
>>  /*
>>    In case easy s(p)lice does Array.p.slice
>>    and creates a new array
>>    (image[begin, end] !== image[begin, end])
>>  */
>>  const pixel = image[begin, end];
>>  manipulate(pixel);
>>  image[begin, end] = pixel;
>> }
>> ```
>
>
> _______________________________________________
> 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: Re: Javascript Language feature Idea

Alican Çubukçuoğlu
In reply to this post by RacerD123
The "easy push" syntax exists in PHP so it will be familiar to at least some people. The slicey thing was also being discussed for PHP. I don't know if it landed or got dropped.

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

Re: JavaScript Language feature Idea

John Gardner
In reply to this post by RacerD123
Using a well-known symbol to access an array's last element is probably wiser than a typical method or property:

let a = [0, 1, 2, 3];
console.log(
a[Symbol.last] === 3
/* true */
);

There're obviously instances where authors have extended Array prototypes with "last" methods or properties, but we can't guarantee they'd all work the same. For instance, assume there are some implementations that skip undefined values:

var a = [0, 1, 2, undefined, undefined];
Array.prototype.last = function(){
return this[this.length - 1];
};
/** One that skips undefined values */
Array.prototype.last = function(){
var offset = 1;
while(offset < this.length && undefined === this[this.length - offset])
++offset;
return this[this.length - offset];
}

These discrepancies are subtle, but have the potential to break backwards compatibility.

Using a well-known symbol eliminates the potential for conflict. Furthermore, it also offers an opportunity to complement any iterable object's ability to synthesise array-like behaviour. For instance, it enables iterables to also return the last object in their list of values:

let pruebas = {
data: ["Probando", "la", "mierda", "esta", undefined],

[Symbol.iterator](){
/* Stuff with .data */
},
[Symbol.last](){
/** Stuff to skip undefined values or whatever */
let offset = 1;
let data   = this.data;
while(
offset < data.length &&
undefined === data[data.length - offset]
)
++offset;
return data[data.length - offset];
}
}


On 23 January 2016 at 08:36, <[hidden email]> wrote:
Send es-discuss mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        https://mail.mozilla.org/listinfo/es-discuss
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of es-discuss digest..."

Today's Topics:

   1. Re: Javascript Language feature Idea (Bradley Meck)
   2. Re: Re: Javascript Language feature Idea (Alican ?ubuk?uo?lu)
   3. Re: Javascript Language feature Idea (kdex)
   4. Re: Javascript Language feature Idea (kdex)
   5. Re: Javascript Language feature Idea (Benoit Marchant)


---------- Forwarded message ----------
From: Bradley Meck <[hidden email]>
To: Dmitry Soshnikov <[hidden email]>
Cc: es-discuss <[hidden email]>
Date: Fri, 22 Jan 2016 13:51:54 -0600
Subject: Re: Javascript Language feature Idea
Personally I prefer a well known symbol for marking you are grabbing from the end of the list rather than this wrapping behavior like D ( https://dlang.org/spec/arrays.html#array-length ). That said I think `.last` is more reasonable than changing prototypes.

On Fri, Jan 22, 2016 at 1:17 PM, Dmitry Soshnikov <[hidden email]> wrote:
On Fri, Jan 22, 2016 at 9:53 AM, RacerD123 <[hidden email]> wrote:
In other programming languages such as python, you can specify the last element of a list with "list[-1]" but in javascript you have to do "list[list.length-1]". Is there maybe a way to make this feature in javascript?


This could be a good addition, although `slice` may work as well. This has been discussed previously as well in the last 4-5 years. In addition with non-enumerable properties you can just monkey-patch `Array.prototype` with the `peek` or `last` method. I guess approach when some library forces some method to widely used, can be a good candidate for adding it to the spec (this how it was with `Function.prototype.bind`, e.g., although today monkey-patching is a bad practice of course).

In addition, you can monkey-patch just `-1`, and other indexes, e.g. a gist from 5 years old, when a similar topic was proposed here: https://gist.github.com/DmitrySoshnikov/984921

Dmitry 

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




---------- Forwarded message ----------
From: "Alican Çubukçuoğlu" <[hidden email]>
To: [hidden email]
Cc: 
Date: Fri, 22 Jan 2016 22:32:42 +0200
Subject: Re: Re: Javascript Language feature Idea
More cool stuff:
```javascript
const arr = [1, 2, 3];

// Easy push
arr[] = 4; // => arr[arr.length];

// Easy s(p)lice
arr[begin, end];,
arr[begin,]; // => arr[begin, arr.length];
arr[begin, end] = [1, 2, 3];
```

A terrible example (terrible because this should be done with WebGL shaders):
```javascript
const image = [ /* Umbagajillion of RGBA pixels */ ];

function manipulate(rgba) {
  rgba[0] += 10;
  rgba[1] += 10;
  rgba[2] += 10;
}

for (let i = 0; i < image.length / 4; i++) {
  const begin = i * 4;
  const end = begin + 4;

  /*
    In case easy s(p)lice doesn't actually Array.p.slice
    and just creates a limited view of the array
    without breaking reference
    (image[begin, end] === image[begin, end])
  */
  manipulate(image[begin, end]);

  /*
    In case easy s(p)lice does Array.p.slice
    and creates a new array
    (image[begin, end] !== image[begin, end])
  */
  const pixel = image[begin, end];
  manipulate(pixel);
  image[begin, end] = pixel;
}
```


---------- Forwarded message ----------
From: kdex <[hidden email]>
To: [hidden email]
Cc: 
Date: Fri, 22 Jan 2016 22:00:01 +0100
Subject: Re: Javascript Language feature Idea
I really adore the slicing syntax. Just as a quick fix, though,
> arr[begin,]; // => arr[begin, arr.length];
should obviously return `arr[begin, arr.length - 1]`.

Honestly, I'm not too sure if the "easy push" syntax is particularly self-
explanatory. There might be better alternatives.

On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
> More cool stuff:
> ```javascript
> const arr = [1, 2, 3];
>
> // Easy push
> arr[] = 4; // => arr[arr.length];
>
> // Easy s(p)lice
> arr[begin, end];,
> arr[begin,]; // => arr[begin, arr.length];
> arr[begin, end] = [1, 2, 3];
> ```
>
> A terrible example (terrible because this should be done with WebGL
> shaders):
> ```javascript
> const image = [ /* Umbagajillion of RGBA pixels */ ];
>
> function manipulate(rgba) {
>   rgba[0] += 10;
>   rgba[1] += 10;
>   rgba[2] += 10;
> }
>
> for (let i = 0; i < image.length / 4; i++) {
>   const begin = i * 4;
>   const end = begin + 4;
>
>   /*
>     In case easy s(p)lice doesn't actually Array.p.slice
>     and just creates a limited view of the array
>     without breaking reference
>     (image[begin, end] === image[begin, end])
>   */
>   manipulate(image[begin, end]);
>
>   /*
>     In case easy s(p)lice does Array.p.slice
>     and creates a new array
>     (image[begin, end] !== image[begin, end])
>   */
>   const pixel = image[begin, end];
>   manipulate(pixel);
>   image[begin, end] = pixel;
> }
> ```





---------- Forwarded message ----------
From: kdex <[hidden email]>
To: [hidden email]
Cc: 
Date: Fri, 22 Jan 2016 22:14:20 +0100
Subject: Re: Javascript Language feature Idea
Standardizing unused array keys breaks compatibility about as much as
extending the prototype does, really. Users can already mangle with both.

The problems are a little more subtle, and yes, it would severely break
backwards compatibility. Consider this example (accessing an element based on
a computed index):

```js
let a = [1, 2, 3];
let occurrence = a.indexOf(4);
a[occurrence];
```

Currently, this should result in `undefined`. After your proposal, you could
introduce a potential bug into every program that accesses arrays using an
index computed with `Array.prototype.indexOf`, since it would now return `3`.

On Freitag, 22. Januar 2016 18:59:06 CET Sander Deryckere wrote:
> Since Array inherits from Object, it can have any key, including the key
> "-1". So something like list[-1] would break compatibility as users can now
> already assign values to the index -1.
>
> If you want a short way to access the last element, it should probably be a
> function in the Array prototype. Something like list.last().
>
> Regards,
> Sander
>
> 2016-01-22 18:53 GMT+01:00 RacerD123 <[hidden email]>:
> > In other programming languages such as python, you can specify the last
> > element of a list with "list[-1]" but in javascript you have to do
> > "list[list.length-1]". Is there maybe a way to make this feature in
> > javascript?
> >
> > _______________________________________________
> > es-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es-discuss





---------- Forwarded message ----------
From: Benoit Marchant <[hidden email]>
To: kdex <[hidden email]>
Cc: [hidden email]
Date: Fri, 22 Jan 2016 13:35:55 -0800
Subject: Re: Javascript Language feature Idea
Interesting! More pedestrian, it would be nice to finally have a version of splice that doesn't create a new other Array as a side effect.

> On Jan 22, 2016, at 13:00, kdex <[hidden email]> wrote:
>
> I really adore the slicing syntax. Just as a quick fix, though,
>> arr[begin,]; // => arr[begin, arr.length];
> should obviously return `arr[begin, arr.length - 1]`.
>
> Honestly, I'm not too sure if the "easy push" syntax is particularly self-
> explanatory. There might be better alternatives.
>
>> On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
>> More cool stuff:
>> ```javascript
>> const arr = [1, 2, 3];
>>
>> // Easy push
>> arr[] = 4; // => arr[arr.length];
>>
>> // Easy s(p)lice
>> arr[begin, end];,
>> arr[begin,]; // => arr[begin, arr.length];
>> arr[begin, end] = [1, 2, 3];
>> ```
>>
>> A terrible example (terrible because this should be done with WebGL
>> shaders):
>> ```javascript
>> const image = [ /* Umbagajillion of RGBA pixels */ ];
>>
>> function manipulate(rgba) {
>>  rgba[0] += 10;
>>  rgba[1] += 10;
>>  rgba[2] += 10;
>> }
>>
>> for (let i = 0; i < image.length / 4; i++) {
>>  const begin = i * 4;
>>  const end = begin + 4;
>>
>>  /*
>>    In case easy s(p)lice doesn't actually Array.p.slice
>>    and just creates a limited view of the array
>>    without breaking reference
>>    (image[begin, end] === image[begin, end])
>>  */
>>  manipulate(image[begin, end]);
>>
>>  /*
>>    In case easy s(p)lice does Array.p.slice
>>    and creates a new array
>>    (image[begin, end] !== image[begin, end])
>>  */
>>  const pixel = image[begin, end];
>>  manipulate(pixel);
>>  image[begin, end] = pixel;
>> }
>> ```
>
>
> _______________________________________________
> 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: JavaScript Language feature Idea

kdex
While John's solution doesn't run into conflicts with downward compatibility,
it still wouldn't solve the problem of getting the n-th last element of an
array. To solve this, it'd probably be a good idea to extend the prototype and
specify a parameter, defaulting to 1.

`Array.prototype.last` doesn't show up a terrible lot on search engines,
either, so we might actually be lucky here. Other than that, I also found two
more threads[1][2] on the EcmaScript discussion archives that propose it.

They might be worth a read.

[1] https://esdiscuss.org/topic/array-prototype-last
[2] https://esdiscuss.org/topic/proposal-array-prototype-last

On Samstag, 23. Januar 2016 15:44:19 CET John Gardner wrote:

> Using a well-known symbol to access an array's last element is probably
> wiser than a typical method or property:
>
> let a = [0, 1, 2, 3];
> console.log(
> a[Symbol.last] === 3
> /* true */
> );
>
> There're obviously instances where authors have extended Array prototypes
> with "last" methods or properties, but we can't guarantee they'd all work
> the same. For instance, assume there are some implementations that skip
> undefined values:
>
> var a = [0, 1, 2, undefined, undefined];
> Array.prototype.last = function(){
> return this[this.length - 1];
> };
> /** One that skips undefined values */
> Array.prototype.last = function(){
> var offset = 1;
> while(offset < this.length && undefined === this[this.length - offset])
> ++offset;
> return this[this.length - offset];
> }
>
> These discrepancies are subtle, but have the potential to break backwards
> compatibility.
>
> Using a well-known symbol eliminates the potential for conflict.
> Furthermore, it also offers an opportunity to complement any iterable
> object's ability to synthesise array-like behaviour. For instance, it
> enables iterables to also return the last object in their list of values:
>
> let pruebas = {
> data: ["Probando", "la", "mierda", "esta", undefined],
>
> [Symbol.iterator](){
> /* Stuff with .data */
> },
> [Symbol.last](){
> /** Stuff to skip undefined values or whatever */
> let offset = 1;
> let data   = this.data;
> while(
> offset < data.length &&
> undefined === data[data.length - offset]
> )
> ++offset;
> return data[data.length - offset];
> }
> }
>
> On 23 January 2016 at 08:36, <[hidden email]> wrote:
> > Send es-discuss mailing list submissions to
> >
> >         [hidden email]
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> >
> >         https://mail.mozilla.org/listinfo/es-discuss
> >
> > or, via email, send a message with subject or body 'help' to
> >
> >         [hidden email]
> >
> > You can reach the person managing the list at
> >
> >         [hidden email]
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of es-discuss digest..."
> >
> > Today's Topics:
> >    1. Re: Javascript Language feature Idea (Bradley Meck)
> >    2. Re: Re: Javascript Language feature Idea (Alican ?ubuk?uo?lu)
> >    3. Re: Javascript Language feature Idea (kdex)
> >    4. Re: Javascript Language feature Idea (kdex)
> >    5. Re: Javascript Language feature Idea (Benoit Marchant)
> >
> > ---------- Forwarded message ----------
> > From: Bradley Meck <[hidden email]>
> > To: Dmitry Soshnikov <[hidden email]>
> > Cc: es-discuss <[hidden email]>
> > Date: Fri, 22 Jan 2016 13:51:54 -0600
> > Subject: Re: Javascript Language feature Idea
> > Personally I prefer a well known symbol for marking you are grabbing from
> > the end of the list rather than this wrapping behavior like D (
> > https://dlang.org/spec/arrays.html#array-length ). That said I think
> > `.last` is more reasonable than changing prototypes.
> >
> > On Fri, Jan 22, 2016 at 1:17 PM, Dmitry Soshnikov <
> >
> > [hidden email]> wrote:
> >> On Fri, Jan 22, 2016 at 9:53 AM, RacerD123 <[hidden email]> wrote:
> >>> In other programming languages such as python, you can specify the last
> >>> element of a list with "list[-1]" but in javascript you have to do
> >>> "list[list.length-1]". Is there maybe a way to make this feature in
> >>> javascript?
> >>
> >> This could be a good addition, although `slice` may work as well. This
> >> has been discussed previously as well in the last 4-5 years. In addition
> >> with non-enumerable properties you can just monkey-patch
> >> `Array.prototype`
> >> with the `peek` or `last` method. I guess approach when some library
> >> forces
> >> some method to widely used, can be a good candidate for adding it to the
> >> spec (this how it was with `Function.prototype.bind`, e.g., although
> >> today
> >> monkey-patching is a bad practice of course).
> >>
> >> In addition, you can monkey-patch just `-1`, and other indexes, e.g. a
> >> gist from 5 years old, when a similar topic was proposed here:
> >> https://gist.github.com/DmitrySoshnikov/984921
> >>
> >> Dmitry
> >>
> >> _______________________________________________
> >> es-discuss mailing list
> >> [hidden email]
> >> https://mail.mozilla.org/listinfo/es-discuss
> >
> > ---------- Forwarded message ----------
> > From: "Alican Çubukçuoğlu" <[hidden email]>
> > To: [hidden email]
> > Cc:
> > Date: Fri, 22 Jan 2016 22:32:42 +0200
> > Subject: Re: Re: Javascript Language feature Idea
> > More cool stuff:
> > ```javascript
> > const arr = [1, 2, 3];
> >
> > // Easy push
> > arr[] = 4; // => arr[arr.length];
> >
> > // Easy s(p)lice
> > arr[begin, end];,
> > arr[begin,]; // => arr[begin, arr.length];
> > arr[begin, end] = [1, 2, 3];
> > ```
> >
> > A terrible example (terrible because this should be done with WebGL
> > shaders):
> > ```javascript
> > const image = [ /* Umbagajillion of RGBA pixels */ ];
> >
> > function manipulate(rgba) {
> >
> >   rgba[0] += 10;
> >   rgba[1] += 10;
> >   rgba[2] += 10;
> >
> > }
> >
> > for (let i = 0; i < image.length / 4; i++) {
> >
> >   const begin = i * 4;
> >   const end = begin + 4;
> >  
> >   /*
> >  
> >     In case easy s(p)lice doesn't actually Array.p.slice
> >     and just creates a limited view of the array
> >     without breaking reference
> >     (image[begin, end] === image[begin, end])
> >  
> >   */
> >   manipulate(image[begin, end]);
> >  
> >   /*
> >  
> >     In case easy s(p)lice does Array.p.slice
> >     and creates a new array
> >     (image[begin, end] !== image[begin, end])
> >  
> >   */
> >   const pixel = image[begin, end];
> >   manipulate(pixel);
> >   image[begin, end] = pixel;
> >
> > }
> > ```
> >
> >
> > ---------- Forwarded message ----------
> > From: kdex <[hidden email]>
> > To: [hidden email]
> > Cc:
> > Date: Fri, 22 Jan 2016 22:00:01 +0100
> > Subject: Re: Javascript Language feature Idea
> > I really adore the slicing syntax. Just as a quick fix, though,
> >
> > > arr[begin,]; // => arr[begin, arr.length];
> >
> > should obviously return `arr[begin, arr.length - 1]`.
> >
> > Honestly, I'm not too sure if the "easy push" syntax is particularly self-
> > explanatory. There might be better alternatives.
> >
> > On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
> > > More cool stuff:
> > > ```javascript
> > > const arr = [1, 2, 3];
> > >
> > > // Easy push
> > > arr[] = 4; // => arr[arr.length];
> > >
> > > // Easy s(p)lice
> > > arr[begin, end];,
> > > arr[begin,]; // => arr[begin, arr.length];
> > > arr[begin, end] = [1, 2, 3];
> > > ```
> > >
> > > A terrible example (terrible because this should be done with WebGL
> > > shaders):
> > > ```javascript
> > > const image = [ /* Umbagajillion of RGBA pixels */ ];
> > >
> > > function manipulate(rgba) {
> > >
> > >   rgba[0] += 10;
> > >   rgba[1] += 10;
> > >   rgba[2] += 10;
> > >
> > > }
> > >
> > > for (let i = 0; i < image.length / 4; i++) {
> > >
> > >   const begin = i * 4;
> > >   const end = begin + 4;
> > >  
> > >   /*
> > >  
> > >     In case easy s(p)lice doesn't actually Array.p.slice
> > >     and just creates a limited view of the array
> > >     without breaking reference
> > >     (image[begin, end] === image[begin, end])
> > >  
> > >   */
> > >   manipulate(image[begin, end]);
> > >  
> > >   /*
> > >  
> > >     In case easy s(p)lice does Array.p.slice
> > >     and creates a new array
> > >     (image[begin, end] !== image[begin, end])
> > >  
> > >   */
> > >   const pixel = image[begin, end];
> > >   manipulate(pixel);
> > >   image[begin, end] = pixel;
> > >
> > > }
> > > ```
> >
> > ---------- Forwarded message ----------
> > From: kdex <[hidden email]>
> > To: [hidden email]
> > Cc:
> > Date: Fri, 22 Jan 2016 22:14:20 +0100
> > Subject: Re: Javascript Language feature Idea
> > Standardizing unused array keys breaks compatibility about as much as
> > extending the prototype does, really. Users can already mangle with both.
> >
> > The problems are a little more subtle, and yes, it would severely break
> > backwards compatibility. Consider this example (accessing an element based
> > on
> > a computed index):
> >
> > ```js
> > let a = [1, 2, 3];
> > let occurrence = a.indexOf(4);
> > a[occurrence];
> > ```
> >
> > Currently, this should result in `undefined`. After your proposal, you
> > could
> > introduce a potential bug into every program that accesses arrays using an
> > index computed with `Array.prototype.indexOf`, since it would now return
> > `3`.
> >
> > On Freitag, 22. Januar 2016 18:59:06 CET Sander Deryckere wrote:
> > > Since Array inherits from Object, it can have any key, including the key
> > > "-1". So something like list[-1] would break compatibility as users can
> >
> > now
> >
> > > already assign values to the index -1.
> > >
> > > If you want a short way to access the last element, it should probably
> >
> > be a
> >
> > > function in the Array prototype. Something like list.last().
> > >
> > > Regards,
> > > Sander
> > >
> > > 2016-01-22 18:53 GMT+01:00 RacerD123 <[hidden email]>:
> > > > In other programming languages such as python, you can specify the
> > > > last
> > > > element of a list with "list[-1]" but in javascript you have to do
> > > > "list[list.length-1]". Is there maybe a way to make this feature in
> > > > javascript?
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> >
> > ---------- Forwarded message ----------
> > From: Benoit Marchant <[hidden email]>
> > To: kdex <[hidden email]>
> > Cc: [hidden email]
> > Date: Fri, 22 Jan 2016 13:35:55 -0800
> > Subject: Re: Javascript Language feature Idea
> > Interesting! More pedestrian, it would be nice to finally have a version
> > of splice that doesn't create a new other Array as a side effect.
> >
> > > On Jan 22, 2016, at 13:00, kdex <[hidden email]> wrote:
> > >
> > > I really adore the slicing syntax. Just as a quick fix, though,
> > >
> > >> arr[begin,]; // => arr[begin, arr.length];
> > >
> > > should obviously return `arr[begin, arr.length - 1]`.
> > >
> > > Honestly, I'm not too sure if the "easy push" syntax is particularly
> >
> > self-
> >
> > > explanatory. There might be better alternatives.
> > >
> > >> On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
> > >> More cool stuff:
> > >> ```javascript
> > >> const arr = [1, 2, 3];
> > >>
> > >> // Easy push
> > >> arr[] = 4; // => arr[arr.length];
> > >>
> > >> // Easy s(p)lice
> > >> arr[begin, end];,
> > >> arr[begin,]; // => arr[begin, arr.length];
> > >> arr[begin, end] = [1, 2, 3];
> > >> ```
> > >>
> > >> A terrible example (terrible because this should be done with WebGL
> > >> shaders):
> > >> ```javascript
> > >> const image = [ /* Umbagajillion of RGBA pixels */ ];
> > >>
> > >> function manipulate(rgba) {
> > >>
> > >>  rgba[0] += 10;
> > >>  rgba[1] += 10;
> > >>  rgba[2] += 10;
> > >>
> > >> }
> > >>
> > >> for (let i = 0; i < image.length / 4; i++) {
> > >>
> > >>  const begin = i * 4;
> > >>  const end = begin + 4;
> > >>  
> > >>  /*
> > >>  
> > >>    In case easy s(p)lice doesn't actually Array.p.slice
> > >>    and just creates a limited view of the array
> > >>    without breaking reference
> > >>    (image[begin, end] === image[begin, end])
> > >>  
> > >>  */
> > >>  manipulate(image[begin, end]);
> > >>  
> > >>  /*
> > >>  
> > >>    In case easy s(p)lice does Array.p.slice
> > >>    and creates a new array
> > >>    (image[begin, end] !== image[begin, end])
> > >>  
> > >>  */
> > >>  const pixel = image[begin, end];
> > >>  manipulate(pixel);
> > >>  image[begin, end] = pixel;
> > >>
> > >> }
> > >> ```
> > >
> > > _______________________________________________
> > > 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: JavaScript Language feature Idea

Thomas
Is this what you're thinking?

Array.prototype.nth = function (n){
  if(n < 0){
    return this[this.length -n];
  } else {
    return this[n];
  }
}

Thomas Foster

@thomasfoster96
Ph: +61477808008
http://thomasfoster.co/

> On 23 Jan 2016, at 4:40 PM, kdex <[hidden email]> wrote:
>
> While John's solution doesn't run into conflicts with downward compatibility,
> it still wouldn't solve the problem of getting the n-th last element of an
> array. To solve this, it'd probably be a good idea to extend the prototype and
> specify a parameter, defaulting to 1.
>
> `Array.prototype.last` doesn't show up a terrible lot on search engines,
> either, so we might actually be lucky here. Other than that, I also found two
> more threads[1][2] on the EcmaScript discussion archives that propose it.
>
> They might be worth a read.
>
> [1] https://esdiscuss.org/topic/array-prototype-last
> [2] https://esdiscuss.org/topic/proposal-array-prototype-last
>
>> On Samstag, 23. Januar 2016 15:44:19 CET John Gardner wrote:
>> Using a well-known symbol to access an array's last element is probably
>> wiser than a typical method or property:
>>
>> let a = [0, 1, 2, 3];
>> console.log(
>> a[Symbol.last] === 3
>> /* true */
>> );
>>
>> There're obviously instances where authors have extended Array prototypes
>> with "last" methods or properties, but we can't guarantee they'd all work
>> the same. For instance, assume there are some implementations that skip
>> undefined values:
>>
>> var a = [0, 1, 2, undefined, undefined];
>> Array.prototype.last = function(){
>> return this[this.length - 1];
>> };
>> /** One that skips undefined values */
>> Array.prototype.last = function(){
>> var offset = 1;
>> while(offset < this.length && undefined === this[this.length - offset])
>> ++offset;
>> return this[this.length - offset];
>> }
>>
>> These discrepancies are subtle, but have the potential to break backwards
>> compatibility.
>>
>> Using a well-known symbol eliminates the potential for conflict.
>> Furthermore, it also offers an opportunity to complement any iterable
>> object's ability to synthesise array-like behaviour. For instance, it
>> enables iterables to also return the last object in their list of values:
>>
>> let pruebas = {
>> data: ["Probando", "la", "mierda", "esta", undefined],
>>
>> [Symbol.iterator](){
>> /* Stuff with .data */
>> },
>> [Symbol.last](){
>> /** Stuff to skip undefined values or whatever */
>> let offset = 1;
>> let data   = this.data;
>> while(
>> offset < data.length &&
>> undefined === data[data.length - offset]
>> )
>> ++offset;
>> return data[data.length - offset];
>> }
>> }
>>
>> O
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: JavaScript Language feature Idea

#!/JoePea
In reply to this post by John Gardner
I think Symbol.last seems like the best option so far, without breaking things.

On Fri, Jan 22, 2016 at 8:44 PM, John Gardner <[hidden email]> wrote:

> Using a well-known symbol to access an array's last element is probably
> wiser than a typical method or property:
>
> let a = [0, 1, 2, 3];
> console.log(
> a[Symbol.last] === 3
> /* true */
> );
>
> There're obviously instances where authors have extended Array prototypes
> with "last" methods or properties, but we can't guarantee they'd all work
> the same. For instance, assume there are some implementations that skip
> undefined values:
>
> var a = [0, 1, 2, undefined, undefined];
> Array.prototype.last = function(){
> return this[this.length - 1];
> };
> /** One that skips undefined values */
> Array.prototype.last = function(){
> var offset = 1;
> while(offset < this.length && undefined === this[this.length - offset])
> ++offset;
> return this[this.length - offset];
> }
>
> These discrepancies are subtle, but have the potential to break backwards
> compatibility.
>
> Using a well-known symbol eliminates the potential for conflict.
> Furthermore, it also offers an opportunity to complement any iterable
> object's ability to synthesise array-like behaviour. For instance, it
> enables iterables to also return the last object in their list of values:
>
> let pruebas = {
> data: ["Probando", "la", "mierda", "esta", undefined],
>
> [Symbol.iterator](){
> /* Stuff with .data */
> },
> [Symbol.last](){
> /** Stuff to skip undefined values or whatever */
> let offset = 1;
> let data   = this.data;
> while(
> offset < data.length &&
> undefined === data[data.length - offset]
> )
> ++offset;
> return data[data.length - offset];
> }
> }
>
>
> On 23 January 2016 at 08:36, <[hidden email]> wrote:
>>
>> Send es-discuss mailing list submissions to
>>         [hidden email]
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>>         https://mail.mozilla.org/listinfo/es-discuss
>> or, via email, send a message with subject or body 'help' to
>>         [hidden email]
>>
>> You can reach the person managing the list at
>>         [hidden email]
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of es-discuss digest..."
>>
>> Today's Topics:
>>
>>    1. Re: Javascript Language feature Idea (Bradley Meck)
>>    2. Re: Re: Javascript Language feature Idea (Alican ?ubuk?uo?lu)
>>    3. Re: Javascript Language feature Idea (kdex)
>>    4. Re: Javascript Language feature Idea (kdex)
>>    5. Re: Javascript Language feature Idea (Benoit Marchant)
>>
>>
>> ---------- Forwarded message ----------
>> From: Bradley Meck <[hidden email]>
>> To: Dmitry Soshnikov <[hidden email]>
>> Cc: es-discuss <[hidden email]>
>> Date: Fri, 22 Jan 2016 13:51:54 -0600
>> Subject: Re: Javascript Language feature Idea
>> Personally I prefer a well known symbol for marking you are grabbing from
>> the end of the list rather than this wrapping behavior like D (
>> https://dlang.org/spec/arrays.html#array-length ). That said I think `.last`
>> is more reasonable than changing prototypes.
>>
>> On Fri, Jan 22, 2016 at 1:17 PM, Dmitry Soshnikov
>> <[hidden email]> wrote:
>>>
>>> On Fri, Jan 22, 2016 at 9:53 AM, RacerD123 <[hidden email]> wrote:
>>>>
>>>> In other programming languages such as python, you can specify the last
>>>> element of a list with "list[-1]" but in javascript you have to do
>>>> "list[list.length-1]". Is there maybe a way to make this feature in
>>>> javascript?
>>>>
>>>
>>> This could be a good addition, although `slice` may work as well. This
>>> has been discussed previously as well in the last 4-5 years. In addition
>>> with non-enumerable properties you can just monkey-patch `Array.prototype`
>>> with the `peek` or `last` method. I guess approach when some library forces
>>> some method to widely used, can be a good candidate for adding it to the
>>> spec (this how it was with `Function.prototype.bind`, e.g., although today
>>> monkey-patching is a bad practice of course).
>>>
>>> In addition, you can monkey-patch just `-1`, and other indexes, e.g. a
>>> gist from 5 years old, when a similar topic was proposed here:
>>> https://gist.github.com/DmitrySoshnikov/984921
>>>
>>> Dmitry
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> [hidden email]
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: "Alican Çubukçuoğlu" <[hidden email]>
>> To: [hidden email]
>> Cc:
>> Date: Fri, 22 Jan 2016 22:32:42 +0200
>> Subject: Re: Re: Javascript Language feature Idea
>> More cool stuff:
>> ```javascript
>> const arr = [1, 2, 3];
>>
>> // Easy push
>> arr[] = 4; // => arr[arr.length];
>>
>> // Easy s(p)lice
>> arr[begin, end];,
>> arr[begin,]; // => arr[begin, arr.length];
>> arr[begin, end] = [1, 2, 3];
>> ```
>>
>> A terrible example (terrible because this should be done with WebGL
>> shaders):
>> ```javascript
>> const image = [ /* Umbagajillion of RGBA pixels */ ];
>>
>> function manipulate(rgba) {
>>   rgba[0] += 10;
>>   rgba[1] += 10;
>>   rgba[2] += 10;
>> }
>>
>> for (let i = 0; i < image.length / 4; i++) {
>>   const begin = i * 4;
>>   const end = begin + 4;
>>
>>   /*
>>     In case easy s(p)lice doesn't actually Array.p.slice
>>     and just creates a limited view of the array
>>     without breaking reference
>>     (image[begin, end] === image[begin, end])
>>   */
>>   manipulate(image[begin, end]);
>>
>>   /*
>>     In case easy s(p)lice does Array.p.slice
>>     and creates a new array
>>     (image[begin, end] !== image[begin, end])
>>   */
>>   const pixel = image[begin, end];
>>   manipulate(pixel);
>>   image[begin, end] = pixel;
>> }
>> ```
>>
>>
>> ---------- Forwarded message ----------
>> From: kdex <[hidden email]>
>> To: [hidden email]
>> Cc:
>> Date: Fri, 22 Jan 2016 22:00:01 +0100
>> Subject: Re: Javascript Language feature Idea
>> I really adore the slicing syntax. Just as a quick fix, though,
>> > arr[begin,]; // => arr[begin, arr.length];
>> should obviously return `arr[begin, arr.length - 1]`.
>>
>> Honestly, I'm not too sure if the "easy push" syntax is particularly self-
>> explanatory. There might be better alternatives.
>>
>> On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
>> > More cool stuff:
>> > ```javascript
>> > const arr = [1, 2, 3];
>> >
>> > // Easy push
>> > arr[] = 4; // => arr[arr.length];
>> >
>> > // Easy s(p)lice
>> > arr[begin, end];,
>> > arr[begin,]; // => arr[begin, arr.length];
>> > arr[begin, end] = [1, 2, 3];
>> > ```
>> >
>> > A terrible example (terrible because this should be done with WebGL
>> > shaders):
>> > ```javascript
>> > const image = [ /* Umbagajillion of RGBA pixels */ ];
>> >
>> > function manipulate(rgba) {
>> >   rgba[0] += 10;
>> >   rgba[1] += 10;
>> >   rgba[2] += 10;
>> > }
>> >
>> > for (let i = 0; i < image.length / 4; i++) {
>> >   const begin = i * 4;
>> >   const end = begin + 4;
>> >
>> >   /*
>> >     In case easy s(p)lice doesn't actually Array.p.slice
>> >     and just creates a limited view of the array
>> >     without breaking reference
>> >     (image[begin, end] === image[begin, end])
>> >   */
>> >   manipulate(image[begin, end]);
>> >
>> >   /*
>> >     In case easy s(p)lice does Array.p.slice
>> >     and creates a new array
>> >     (image[begin, end] !== image[begin, end])
>> >   */
>> >   const pixel = image[begin, end];
>> >   manipulate(pixel);
>> >   image[begin, end] = pixel;
>> > }
>> > ```
>>
>>
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: kdex <[hidden email]>
>> To: [hidden email]
>> Cc:
>> Date: Fri, 22 Jan 2016 22:14:20 +0100
>> Subject: Re: Javascript Language feature Idea
>> Standardizing unused array keys breaks compatibility about as much as
>> extending the prototype does, really. Users can already mangle with both.
>>
>> The problems are a little more subtle, and yes, it would severely break
>> backwards compatibility. Consider this example (accessing an element based
>> on
>> a computed index):
>>
>> ```js
>> let a = [1, 2, 3];
>> let occurrence = a.indexOf(4);
>> a[occurrence];
>> ```
>>
>> Currently, this should result in `undefined`. After your proposal, you
>> could
>> introduce a potential bug into every program that accesses arrays using an
>> index computed with `Array.prototype.indexOf`, since it would now return
>> `3`.
>>
>> On Freitag, 22. Januar 2016 18:59:06 CET Sander Deryckere wrote:
>> > Since Array inherits from Object, it can have any key, including the key
>> > "-1". So something like list[-1] would break compatibility as users can
>> > now
>> > already assign values to the index -1.
>> >
>> > If you want a short way to access the last element, it should probably
>> > be a
>> > function in the Array prototype. Something like list.last().
>> >
>> > Regards,
>> > Sander
>> >
>> > 2016-01-22 18:53 GMT+01:00 RacerD123 <[hidden email]>:
>> > > In other programming languages such as python, you can specify the
>> > > last
>> > > element of a list with "list[-1]" but in javascript you have to do
>> > > "list[list.length-1]". Is there maybe a way to make this feature in
>> > > javascript?
>> > >
>> > > _______________________________________________
>> > > es-discuss mailing list
>> > > [hidden email]
>> > > https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: Benoit Marchant <[hidden email]>
>> To: kdex <[hidden email]>
>> Cc: [hidden email]
>> Date: Fri, 22 Jan 2016 13:35:55 -0800
>> Subject: Re: Javascript Language feature Idea
>> Interesting! More pedestrian, it would be nice to finally have a version
>> of splice that doesn't create a new other Array as a side effect.
>>
>> > On Jan 22, 2016, at 13:00, kdex <[hidden email]> wrote:
>> >
>> > I really adore the slicing syntax. Just as a quick fix, though,
>> >> arr[begin,]; // => arr[begin, arr.length];
>> > should obviously return `arr[begin, arr.length - 1]`.
>> >
>> > Honestly, I'm not too sure if the "easy push" syntax is particularly
>> > self-
>> > explanatory. There might be better alternatives.
>> >
>> >> On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
>> >> More cool stuff:
>> >> ```javascript
>> >> const arr = [1, 2, 3];
>> >>
>> >> // Easy push
>> >> arr[] = 4; // => arr[arr.length];
>> >>
>> >> // Easy s(p)lice
>> >> arr[begin, end];,
>> >> arr[begin,]; // => arr[begin, arr.length];
>> >> arr[begin, end] = [1, 2, 3];
>> >> ```
>> >>
>> >> A terrible example (terrible because this should be done with WebGL
>> >> shaders):
>> >> ```javascript
>> >> const image = [ /* Umbagajillion of RGBA pixels */ ];
>> >>
>> >> function manipulate(rgba) {
>> >>  rgba[0] += 10;
>> >>  rgba[1] += 10;
>> >>  rgba[2] += 10;
>> >> }
>> >>
>> >> for (let i = 0; i < image.length / 4; i++) {
>> >>  const begin = i * 4;
>> >>  const end = begin + 4;
>> >>
>> >>  /*
>> >>    In case easy s(p)lice doesn't actually Array.p.slice
>> >>    and just creates a limited view of the array
>> >>    without breaking reference
>> >>    (image[begin, end] === image[begin, end])
>> >>  */
>> >>  manipulate(image[begin, end]);
>> >>
>> >>  /*
>> >>    In case easy s(p)lice does Array.p.slice
>> >>    and creates a new array
>> >>    (image[begin, end] !== image[begin, end])
>> >>  */
>> >>  const pixel = image[begin, end];
>> >>  manipulate(pixel);
>> >>  image[begin, end] = pixel;
>> >> }
>> >> ```
>> >
>> >
>> > _______________________________________________
>> > 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: JavaScript Language feature Idea

John Gardner
In reply to this post by RacerD123
Thomas is right. "last" should unambiguously mean **last**. If authors want to ascertain the second-last, or third-last, a different method is probably preferable. "nth" sounds great, and could work from the end of the array if passed a negative value.

This would circumvent the issues raised by overriding the -1 property a.l.a Python.

On 23 January 2016 at 17:04, <[hidden email]> wrote:
Send es-discuss mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        https://mail.mozilla.org/listinfo/es-discuss
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of es-discuss digest..."

Today's Topics:

   1. Re: JavaScript Language feature Idea (kdex)
   2. Re: JavaScript Language feature Idea (Thomas)
   3. Re: JavaScript Language feature Idea (/#!/JoePea)


---------- Forwarded message ----------
From: kdex <[hidden email]>
To: [hidden email]
Cc: 
Date: Sat, 23 Jan 2016 06:40:28 +0100
Subject: Re: JavaScript Language feature Idea
While John's solution doesn't run into conflicts with downward compatibility,
it still wouldn't solve the problem of getting the n-th last element of an
array. To solve this, it'd probably be a good idea to extend the prototype and
specify a parameter, defaulting to 1.

`Array.prototype.last` doesn't show up a terrible lot on search engines,
either, so we might actually be lucky here. Other than that, I also found two
more threads[1][2] on the EcmaScript discussion archives that propose it.

They might be worth a read.

[1] https://esdiscuss.org/topic/array-prototype-last
[2] https://esdiscuss.org/topic/proposal-array-prototype-last

On Samstag, 23. Januar 2016 15:44:19 CET John Gardner wrote:
> Using a well-known symbol to access an array's last element is probably
> wiser than a typical method or property:
>
> let a = [0, 1, 2, 3];
> console.log(
> a[Symbol.last] === 3
> /* true */
> );
>
> There're obviously instances where authors have extended Array prototypes
> with "last" methods or properties, but we can't guarantee they'd all work
> the same. For instance, assume there are some implementations that skip
> undefined values:
>
> var a = [0, 1, 2, undefined, undefined];
> Array.prototype.last = function(){
> return this[this.length - 1];
> };
> /** One that skips undefined values */
> Array.prototype.last = function(){
> var offset = 1;
> while(offset < this.length && undefined === this[this.length - offset])
> ++offset;
> return this[this.length - offset];
> }
>
> These discrepancies are subtle, but have the potential to break backwards
> compatibility.
>
> Using a well-known symbol eliminates the potential for conflict.
> Furthermore, it also offers an opportunity to complement any iterable
> object's ability to synthesise array-like behaviour. For instance, it
> enables iterables to also return the last object in their list of values:
>
> let pruebas = {
> data: ["Probando", "la", "mierda", "esta", undefined],
>
> [Symbol.iterator](){
> /* Stuff with .data */
> },
> [Symbol.last](){
> /** Stuff to skip undefined values or whatever */
> let offset = 1;
> let data   = this.data;
> while(
> offset < data.length &&
> undefined === data[data.length - offset]
> )
> ++offset;
> return data[data.length - offset];
> }
> }
>
> On 23 January 2016 at 08:36, <[hidden email]> wrote:
> > Send es-discuss mailing list submissions to
> >
> >         [hidden email]
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> >
> >         https://mail.mozilla.org/listinfo/es-discuss
> >
> > or, via email, send a message with subject or body 'help' to
> >
> >         [hidden email]
> >
> > You can reach the person managing the list at
> >
> >         [hidden email]
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of es-discuss digest..."
> >
> > Today's Topics:
> >    1. Re: Javascript Language feature Idea (Bradley Meck)
> >    2. Re: Re: Javascript Language feature Idea (Alican ?ubuk?uo?lu)
> >    3. Re: Javascript Language feature Idea (kdex)
> >    4. Re: Javascript Language feature Idea (kdex)
> >    5. Re: Javascript Language feature Idea (Benoit Marchant)
> >
> > ---------- Forwarded message ----------
> > From: Bradley Meck <[hidden email]>
> > To: Dmitry Soshnikov <[hidden email]>
> > Cc: es-discuss <[hidden email]>
> > Date: Fri, 22 Jan 2016 13:51:54 -0600
> > Subject: Re: Javascript Language feature Idea
> > Personally I prefer a well known symbol for marking you are grabbing from
> > the end of the list rather than this wrapping behavior like D (
> > https://dlang.org/spec/arrays.html#array-length ). That said I think
> > `.last` is more reasonable than changing prototypes.
> >
> > On Fri, Jan 22, 2016 at 1:17 PM, Dmitry Soshnikov <
> >
> > [hidden email]> wrote:
> >> On Fri, Jan 22, 2016 at 9:53 AM, RacerD123 <[hidden email]> wrote:
> >>> In other programming languages such as python, you can specify the last
> >>> element of a list with "list[-1]" but in javascript you have to do
> >>> "list[list.length-1]". Is there maybe a way to make this feature in
> >>> javascript?
> >>
> >> This could be a good addition, although `slice` may work as well. This
> >> has been discussed previously as well in the last 4-5 years. In addition
> >> with non-enumerable properties you can just monkey-patch
> >> `Array.prototype`
> >> with the `peek` or `last` method. I guess approach when some library
> >> forces
> >> some method to widely used, can be a good candidate for adding it to the
> >> spec (this how it was with `Function.prototype.bind`, e.g., although
> >> today
> >> monkey-patching is a bad practice of course).
> >>
> >> In addition, you can monkey-patch just `-1`, and other indexes, e.g. a
> >> gist from 5 years old, when a similar topic was proposed here:
> >> https://gist.github.com/DmitrySoshnikov/984921
> >>
> >> Dmitry
> >>
> >> _______________________________________________
> >> es-discuss mailing list
> >> [hidden email]
> >> https://mail.mozilla.org/listinfo/es-discuss
> >
> > ---------- Forwarded message ----------
> > From: "Alican Çubukçuoğlu" <[hidden email]>
> > To: [hidden email]
> > Cc:
> > Date: Fri, 22 Jan 2016 22:32:42 +0200
> > Subject: Re: Re: Javascript Language feature Idea
> > More cool stuff:
> > ```javascript
> > const arr = [1, 2, 3];
> >
> > // Easy push
> > arr[] = 4; // => arr[arr.length];
> >
> > // Easy s(p)lice
> > arr[begin, end];,
> > arr[begin,]; // => arr[begin, arr.length];
> > arr[begin, end] = [1, 2, 3];
> > ```
> >
> > A terrible example (terrible because this should be done with WebGL
> > shaders):
> > ```javascript
> > const image = [ /* Umbagajillion of RGBA pixels */ ];
> >
> > function manipulate(rgba) {
> >
> >   rgba[0] += 10;
> >   rgba[1] += 10;
> >   rgba[2] += 10;
> >
> > }
> >
> > for (let i = 0; i < image.length / 4; i++) {
> >
> >   const begin = i * 4;
> >   const end = begin + 4;
> >
> >   /*
> >
> >     In case easy s(p)lice doesn't actually Array.p.slice
> >     and just creates a limited view of the array
> >     without breaking reference
> >     (image[begin, end] === image[begin, end])
> >
> >   */
> >   manipulate(image[begin, end]);
> >
> >   /*
> >
> >     In case easy s(p)lice does Array.p.slice
> >     and creates a new array
> >     (image[begin, end] !== image[begin, end])
> >
> >   */
> >   const pixel = image[begin, end];
> >   manipulate(pixel);
> >   image[begin, end] = pixel;
> >
> > }
> > ```
> >
> >
> > ---------- Forwarded message ----------
> > From: kdex <[hidden email]>
> > To: [hidden email]
> > Cc:
> > Date: Fri, 22 Jan 2016 22:00:01 +0100
> > Subject: Re: Javascript Language feature Idea
> > I really adore the slicing syntax. Just as a quick fix, though,
> >
> > > arr[begin,]; // => arr[begin, arr.length];
> >
> > should obviously return `arr[begin, arr.length - 1]`.
> >
> > Honestly, I'm not too sure if the "easy push" syntax is particularly self-
> > explanatory. There might be better alternatives.
> >
> > On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
> > > More cool stuff:
> > > ```javascript
> > > const arr = [1, 2, 3];
> > >
> > > // Easy push
> > > arr[] = 4; // => arr[arr.length];
> > >
> > > // Easy s(p)lice
> > > arr[begin, end];,
> > > arr[begin,]; // => arr[begin, arr.length];
> > > arr[begin, end] = [1, 2, 3];
> > > ```
> > >
> > > A terrible example (terrible because this should be done with WebGL
> > > shaders):
> > > ```javascript
> > > const image = [ /* Umbagajillion of RGBA pixels */ ];
> > >
> > > function manipulate(rgba) {
> > >
> > >   rgba[0] += 10;
> > >   rgba[1] += 10;
> > >   rgba[2] += 10;
> > >
> > > }
> > >
> > > for (let i = 0; i < image.length / 4; i++) {
> > >
> > >   const begin = i * 4;
> > >   const end = begin + 4;
> > >
> > >   /*
> > >
> > >     In case easy s(p)lice doesn't actually Array.p.slice
> > >     and just creates a limited view of the array
> > >     without breaking reference
> > >     (image[begin, end] === image[begin, end])
> > >
> > >   */
> > >   manipulate(image[begin, end]);
> > >
> > >   /*
> > >
> > >     In case easy s(p)lice does Array.p.slice
> > >     and creates a new array
> > >     (image[begin, end] !== image[begin, end])
> > >
> > >   */
> > >   const pixel = image[begin, end];
> > >   manipulate(pixel);
> > >   image[begin, end] = pixel;
> > >
> > > }
> > > ```
> >
> > ---------- Forwarded message ----------
> > From: kdex <[hidden email]>
> > To: [hidden email]
> > Cc:
> > Date: Fri, 22 Jan 2016 22:14:20 +0100
> > Subject: Re: Javascript Language feature Idea
> > Standardizing unused array keys breaks compatibility about as much as
> > extending the prototype does, really. Users can already mangle with both.
> >
> > The problems are a little more subtle, and yes, it would severely break
> > backwards compatibility. Consider this example (accessing an element based
> > on
> > a computed index):
> >
> > ```js
> > let a = [1, 2, 3];
> > let occurrence = a.indexOf(4);
> > a[occurrence];
> > ```
> >
> > Currently, this should result in `undefined`. After your proposal, you
> > could
> > introduce a potential bug into every program that accesses arrays using an
> > index computed with `Array.prototype.indexOf`, since it would now return
> > `3`.
> >
> > On Freitag, 22. Januar 2016 18:59:06 CET Sander Deryckere wrote:
> > > Since Array inherits from Object, it can have any key, including the key
> > > "-1". So something like list[-1] would break compatibility as users can
> >
> > now
> >
> > > already assign values to the index -1.
> > >
> > > If you want a short way to access the last element, it should probably
> >
> > be a
> >
> > > function in the Array prototype. Something like list.last().
> > >
> > > Regards,
> > > Sander
> > >
> > > 2016-01-22 18:53 GMT+01:00 RacerD123 <[hidden email]>:
> > > > In other programming languages such as python, you can specify the
> > > > last
> > > > element of a list with "list[-1]" but in javascript you have to do
> > > > "list[list.length-1]". Is there maybe a way to make this feature in
> > > > javascript?
> > > >
> > > > _______________________________________________
> > > > es-discuss mailing list
> > > > [hidden email]
> > > > https://mail.mozilla.org/listinfo/es-discuss
> >
> > ---------- Forwarded message ----------
> > From: Benoit Marchant <[hidden email]>
> > To: kdex <[hidden email]>
> > Cc: [hidden email]
> > Date: Fri, 22 Jan 2016 13:35:55 -0800
> > Subject: Re: Javascript Language feature Idea
> > Interesting! More pedestrian, it would be nice to finally have a version
> > of splice that doesn't create a new other Array as a side effect.
> >
> > > On Jan 22, 2016, at 13:00, kdex <[hidden email]> wrote:
> > >
> > > I really adore the slicing syntax. Just as a quick fix, though,
> > >
> > >> arr[begin,]; // => arr[begin, arr.length];
> > >
> > > should obviously return `arr[begin, arr.length - 1]`.
> > >
> > > Honestly, I'm not too sure if the "easy push" syntax is particularly
> >
> > self-
> >
> > > explanatory. There might be better alternatives.
> > >
> > >> On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
> > >> More cool stuff:
> > >> ```javascript
> > >> const arr = [1, 2, 3];
> > >>
> > >> // Easy push
> > >> arr[] = 4; // => arr[arr.length];
> > >>
> > >> // Easy s(p)lice
> > >> arr[begin, end];,
> > >> arr[begin,]; // => arr[begin, arr.length];
> > >> arr[begin, end] = [1, 2, 3];
> > >> ```
> > >>
> > >> A terrible example (terrible because this should be done with WebGL
> > >> shaders):
> > >> ```javascript
> > >> const image = [ /* Umbagajillion of RGBA pixels */ ];
> > >>
> > >> function manipulate(rgba) {
> > >>
> > >>  rgba[0] += 10;
> > >>  rgba[1] += 10;
> > >>  rgba[2] += 10;
> > >>
> > >> }
> > >>
> > >> for (let i = 0; i < image.length / 4; i++) {
> > >>
> > >>  const begin = i * 4;
> > >>  const end = begin + 4;
> > >>
> > >>  /*
> > >>
> > >>    In case easy s(p)lice doesn't actually Array.p.slice
> > >>    and just creates a limited view of the array
> > >>    without breaking reference
> > >>    (image[begin, end] === image[begin, end])
> > >>
> > >>  */
> > >>  manipulate(image[begin, end]);
> > >>
> > >>  /*
> > >>
> > >>    In case easy s(p)lice does Array.p.slice
> > >>    and creates a new array
> > >>    (image[begin, end] !== image[begin, end])
> > >>
> > >>  */
> > >>  const pixel = image[begin, end];
> > >>  manipulate(pixel);
> > >>  image[begin, end] = pixel;
> > >>
> > >> }
> > >> ```
> > >
> > > _______________________________________________
> > > 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





---------- Forwarded message ----------
From: Thomas <[hidden email]>
To: kdex <[hidden email]>
Cc: [hidden email]
Date: Sat, 23 Jan 2016 16:51:21 +1100
Subject: Re: JavaScript Language feature Idea
Is this what you're thinking?

Array.prototype.nth = function (n){
  if(n < 0){
    return this[this.length -n];
  } else {
    return this[n];
  }
}

Thomas Foster

@thomasfoster96
Ph: <a href="tel:%2B61477808008" value="+61477808008">+61477808008
http://thomasfoster.co/

> On 23 Jan 2016, at 4:40 PM, kdex <[hidden email]> wrote:
>
> While John's solution doesn't run into conflicts with downward compatibility,
> it still wouldn't solve the problem of getting the n-th last element of an
> array. To solve this, it'd probably be a good idea to extend the prototype and
> specify a parameter, defaulting to 1.
>
> `Array.prototype.last` doesn't show up a terrible lot on search engines,
> either, so we might actually be lucky here. Other than that, I also found two
> more threads[1][2] on the EcmaScript discussion archives that propose it.
>
> They might be worth a read.
>
> [1] https://esdiscuss.org/topic/array-prototype-last
> [2] https://esdiscuss.org/topic/proposal-array-prototype-last
>
>> On Samstag, 23. Januar 2016 15:44:19 CET John Gardner wrote:
>> Using a well-known symbol to access an array's last element is probably
>> wiser than a typical method or property:
>>
>> let a = [0, 1, 2, 3];
>> console.log(
>> a[Symbol.last] === 3
>> /* true */
>> );
>>
>> There're obviously instances where authors have extended Array prototypes
>> with "last" methods or properties, but we can't guarantee they'd all work
>> the same. For instance, assume there are some implementations that skip
>> undefined values:
>>
>> var a = [0, 1, 2, undefined, undefined];
>> Array.prototype.last = function(){
>> return this[this.length - 1];
>> };
>> /** One that skips undefined values */
>> Array.prototype.last = function(){
>> var offset = 1;
>> while(offset < this.length && undefined === this[this.length - offset])
>> ++offset;
>> return this[this.length - offset];
>> }
>>
>> These discrepancies are subtle, but have the potential to break backwards
>> compatibility.
>>
>> Using a well-known symbol eliminates the potential for conflict.
>> Furthermore, it also offers an opportunity to complement any iterable
>> object's ability to synthesise array-like behaviour. For instance, it
>> enables iterables to also return the last object in their list of values:
>>
>> let pruebas = {
>> data: ["Probando", "la", "mierda", "esta", undefined],
>>
>> [Symbol.iterator](){
>> /* Stuff with .data */
>> },
>> [Symbol.last](){
>> /** Stuff to skip undefined values or whatever */
>> let offset = 1;
>> let data   = this.data;
>> while(
>> offset < data.length &&
>> undefined === data[data.length - offset]
>> )
>> ++offset;
>> return data[data.length - offset];
>> }
>> }
>>
>> O



---------- Forwarded message ----------
From: "/#!/JoePea" <[hidden email]>
To: John Gardner <[hidden email]>
Cc: es-discuss <[hidden email]>
Date: Fri, 22 Jan 2016 22:04:14 -0800
Subject: Re: JavaScript Language feature Idea
I think Symbol.last seems like the best option so far, without breaking things.

On Fri, Jan 22, 2016 at 8:44 PM, John Gardner <[hidden email]> wrote:
> Using a well-known symbol to access an array's last element is probably
> wiser than a typical method or property:
>
> let a = [0, 1, 2, 3];
> console.log(
> a[Symbol.last] === 3
> /* true */
> );
>
> There're obviously instances where authors have extended Array prototypes
> with "last" methods or properties, but we can't guarantee they'd all work
> the same. For instance, assume there are some implementations that skip
> undefined values:
>
> var a = [0, 1, 2, undefined, undefined];
> Array.prototype.last = function(){
> return this[this.length - 1];
> };
> /** One that skips undefined values */
> Array.prototype.last = function(){
> var offset = 1;
> while(offset < this.length && undefined === this[this.length - offset])
> ++offset;
> return this[this.length - offset];
> }
>
> These discrepancies are subtle, but have the potential to break backwards
> compatibility.
>
> Using a well-known symbol eliminates the potential for conflict.
> Furthermore, it also offers an opportunity to complement any iterable
> object's ability to synthesise array-like behaviour. For instance, it
> enables iterables to also return the last object in their list of values:
>
> let pruebas = {
> data: ["Probando", "la", "mierda", "esta", undefined],
>
> [Symbol.iterator](){
> /* Stuff with .data */
> },
> [Symbol.last](){
> /** Stuff to skip undefined values or whatever */
> let offset = 1;
> let data   = this.data;
> while(
> offset < data.length &&
> undefined === data[data.length - offset]
> )
> ++offset;
> return data[data.length - offset];
> }
> }
>
>
> On 23 January 2016 at 08:36, <[hidden email]> wrote:
>>
>> Send es-discuss mailing list submissions to
>>         [hidden email]
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>>         https://mail.mozilla.org/listinfo/es-discuss
>> or, via email, send a message with subject or body 'help' to
>>         [hidden email]
>>
>> You can reach the person managing the list at
>>         [hidden email]
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of es-discuss digest..."
>>
>> Today's Topics:
>>
>>    1. Re: Javascript Language feature Idea (Bradley Meck)
>>    2. Re: Re: Javascript Language feature Idea (Alican ?ubuk?uo?lu)
>>    3. Re: Javascript Language feature Idea (kdex)
>>    4. Re: Javascript Language feature Idea (kdex)
>>    5. Re: Javascript Language feature Idea (Benoit Marchant)
>>
>>
>> ---------- Forwarded message ----------
>> From: Bradley Meck <[hidden email]>
>> To: Dmitry Soshnikov <[hidden email]>
>> Cc: es-discuss <[hidden email]>
>> Date: Fri, 22 Jan 2016 13:51:54 -0600
>> Subject: Re: Javascript Language feature Idea
>> Personally I prefer a well known symbol for marking you are grabbing from
>> the end of the list rather than this wrapping behavior like D (
>> https://dlang.org/spec/arrays.html#array-length ). That said I think `.last`
>> is more reasonable than changing prototypes.
>>
>> On Fri, Jan 22, 2016 at 1:17 PM, Dmitry Soshnikov
>> <[hidden email]> wrote:
>>>
>>> On Fri, Jan 22, 2016 at 9:53 AM, RacerD123 <[hidden email]> wrote:
>>>>
>>>> In other programming languages such as python, you can specify the last
>>>> element of a list with "list[-1]" but in javascript you have to do
>>>> "list[list.length-1]". Is there maybe a way to make this feature in
>>>> javascript?
>>>>
>>>
>>> This could be a good addition, although `slice` may work as well. This
>>> has been discussed previously as well in the last 4-5 years. In addition
>>> with non-enumerable properties you can just monkey-patch `Array.prototype`
>>> with the `peek` or `last` method. I guess approach when some library forces
>>> some method to widely used, can be a good candidate for adding it to the
>>> spec (this how it was with `Function.prototype.bind`, e.g., although today
>>> monkey-patching is a bad practice of course).
>>>
>>> In addition, you can monkey-patch just `-1`, and other indexes, e.g. a
>>> gist from 5 years old, when a similar topic was proposed here:
>>> https://gist.github.com/DmitrySoshnikov/984921
>>>
>>> Dmitry
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> [hidden email]
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: "Alican Çubukçuoğlu" <[hidden email]>
>> To: [hidden email]
>> Cc:
>> Date: Fri, 22 Jan 2016 22:32:42 +0200
>> Subject: Re: Re: Javascript Language feature Idea
>> More cool stuff:
>> ```javascript
>> const arr = [1, 2, 3];
>>
>> // Easy push
>> arr[] = 4; // => arr[arr.length];
>>
>> // Easy s(p)lice
>> arr[begin, end];,
>> arr[begin,]; // => arr[begin, arr.length];
>> arr[begin, end] = [1, 2, 3];
>> ```
>>
>> A terrible example (terrible because this should be done with WebGL
>> shaders):
>> ```javascript
>> const image = [ /* Umbagajillion of RGBA pixels */ ];
>>
>> function manipulate(rgba) {
>>   rgba[0] += 10;
>>   rgba[1] += 10;
>>   rgba[2] += 10;
>> }
>>
>> for (let i = 0; i < image.length / 4; i++) {
>>   const begin = i * 4;
>>   const end = begin + 4;
>>
>>   /*
>>     In case easy s(p)lice doesn't actually Array.p.slice
>>     and just creates a limited view of the array
>>     without breaking reference
>>     (image[begin, end] === image[begin, end])
>>   */
>>   manipulate(image[begin, end]);
>>
>>   /*
>>     In case easy s(p)lice does Array.p.slice
>>     and creates a new array
>>     (image[begin, end] !== image[begin, end])
>>   */
>>   const pixel = image[begin, end];
>>   manipulate(pixel);
>>   image[begin, end] = pixel;
>> }
>> ```
>>
>>
>> ---------- Forwarded message ----------
>> From: kdex <[hidden email]>
>> To: [hidden email]
>> Cc:
>> Date: Fri, 22 Jan 2016 22:00:01 +0100
>> Subject: Re: Javascript Language feature Idea
>> I really adore the slicing syntax. Just as a quick fix, though,
>> > arr[begin,]; // => arr[begin, arr.length];
>> should obviously return `arr[begin, arr.length - 1]`.
>>
>> Honestly, I'm not too sure if the "easy push" syntax is particularly self-
>> explanatory. There might be better alternatives.
>>
>> On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
>> > More cool stuff:
>> > ```javascript
>> > const arr = [1, 2, 3];
>> >
>> > // Easy push
>> > arr[] = 4; // => arr[arr.length];
>> >
>> > // Easy s(p)lice
>> > arr[begin, end];,
>> > arr[begin,]; // => arr[begin, arr.length];
>> > arr[begin, end] = [1, 2, 3];
>> > ```
>> >
>> > A terrible example (terrible because this should be done with WebGL
>> > shaders):
>> > ```javascript
>> > const image = [ /* Umbagajillion of RGBA pixels */ ];
>> >
>> > function manipulate(rgba) {
>> >   rgba[0] += 10;
>> >   rgba[1] += 10;
>> >   rgba[2] += 10;
>> > }
>> >
>> > for (let i = 0; i < image.length / 4; i++) {
>> >   const begin = i * 4;
>> >   const end = begin + 4;
>> >
>> >   /*
>> >     In case easy s(p)lice doesn't actually Array.p.slice
>> >     and just creates a limited view of the array
>> >     without breaking reference
>> >     (image[begin, end] === image[begin, end])
>> >   */
>> >   manipulate(image[begin, end]);
>> >
>> >   /*
>> >     In case easy s(p)lice does Array.p.slice
>> >     and creates a new array
>> >     (image[begin, end] !== image[begin, end])
>> >   */
>> >   const pixel = image[begin, end];
>> >   manipulate(pixel);
>> >   image[begin, end] = pixel;
>> > }
>> > ```
>>
>>
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: kdex <[hidden email]>
>> To: [hidden email]
>> Cc:
>> Date: Fri, 22 Jan 2016 22:14:20 +0100
>> Subject: Re: Javascript Language feature Idea
>> Standardizing unused array keys breaks compatibility about as much as
>> extending the prototype does, really. Users can already mangle with both.
>>
>> The problems are a little more subtle, and yes, it would severely break
>> backwards compatibility. Consider this example (accessing an element based
>> on
>> a computed index):
>>
>> ```js
>> let a = [1, 2, 3];
>> let occurrence = a.indexOf(4);
>> a[occurrence];
>> ```
>>
>> Currently, this should result in `undefined`. After your proposal, you
>> could
>> introduce a potential bug into every program that accesses arrays using an
>> index computed with `Array.prototype.indexOf`, since it would now return
>> `3`.
>>
>> On Freitag, 22. Januar 2016 18:59:06 CET Sander Deryckere wrote:
>> > Since Array inherits from Object, it can have any key, including the key
>> > "-1". So something like list[-1] would break compatibility as users can
>> > now
>> > already assign values to the index -1.
>> >
>> > If you want a short way to access the last element, it should probably
>> > be a
>> > function in the Array prototype. Something like list.last().
>> >
>> > Regards,
>> > Sander
>> >
>> > 2016-01-22 18:53 GMT+01:00 RacerD123 <[hidden email]>:
>> > > In other programming languages such as python, you can specify the
>> > > last
>> > > element of a list with "list[-1]" but in javascript you have to do
>> > > "list[list.length-1]". Is there maybe a way to make this feature in
>> > > javascript?
>> > >
>> > > _______________________________________________
>> > > es-discuss mailing list
>> > > [hidden email]
>> > > https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>>
>>
>>
>> ---------- Forwarded message ----------
>> From: Benoit Marchant <[hidden email]>
>> To: kdex <[hidden email]>
>> Cc: [hidden email]
>> Date: Fri, 22 Jan 2016 13:35:55 -0800
>> Subject: Re: Javascript Language feature Idea
>> Interesting! More pedestrian, it would be nice to finally have a version
>> of splice that doesn't create a new other Array as a side effect.
>>
>> > On Jan 22, 2016, at 13:00, kdex <[hidden email]> wrote:
>> >
>> > I really adore the slicing syntax. Just as a quick fix, though,
>> >> arr[begin,]; // => arr[begin, arr.length];
>> > should obviously return `arr[begin, arr.length - 1]`.
>> >
>> > Honestly, I'm not too sure if the "easy push" syntax is particularly
>> > self-
>> > explanatory. There might be better alternatives.
>> >
>> >> On Freitag, 22. Januar 2016 22:32:42 CET Alican Çubukçuoğlu wrote:
>> >> More cool stuff:
>> >> ```javascript
>> >> const arr = [1, 2, 3];
>> >>
>> >> // Easy push
>> >> arr[] = 4; // => arr[arr.length];
>> >>
>> >> // Easy s(p)lice
>> >> arr[begin, end];,
>> >> arr[begin,]; // => arr[begin, arr.length];
>> >> arr[begin, end] = [1, 2, 3];
>> >> ```
>> >>
>> >> A terrible example (terrible because this should be done with WebGL
>> >> shaders):
>> >> ```javascript
>> >> const image = [ /* Umbagajillion of RGBA pixels */ ];
>> >>
>> >> function manipulate(rgba) {
>> >>  rgba[0] += 10;
>> >>  rgba[1] += 10;
>> >>  rgba[2] += 10;
>> >> }
>> >>
>> >> for (let i = 0; i < image.length / 4; i++) {
>> >>  const begin = i * 4;
>> >>  const end = begin + 4;
>> >>
>> >>  /*
>> >>    In case easy s(p)lice doesn't actually Array.p.slice
>> >>    and just creates a limited view of the array
>> >>    without breaking reference
>> >>    (image[begin, end] === image[begin, end])
>> >>  */
>> >>  manipulate(image[begin, end]);
>> >>
>> >>  /*
>> >>    In case easy s(p)lice does Array.p.slice
>> >>    and creates a new array
>> >>    (image[begin, end] !== image[begin, end])
>> >>  */
>> >>  const pixel = image[begin, end];
>> >>  manipulate(pixel);
>> >>  image[begin, end] = pixel;
>> >> }
>> >> ```
>> >
>> >
>> > _______________________________________________
>> > 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: JavaScript Language feature Idea

kdex
In reply to this post by Thomas
@Thomas: I think your implementation has a bug for negative values of n. It
should have been `+ n` instead of `-n`, probably.

To me, It would feel a little more "natural" if it behaved somewhere around
this:

```js
/* Used like the [] operator, but indices are counted from right to left and
also works with negative numbers */
Array.prototype.last = function(n) {
        if (n === undefined) {
                n = 0;
        }
        else if (typeof n !== "number") {
                throw new Error("First argument must be a number");
        }
        if (n < 0) {
                return this[Math.abs(n) - 1];
        }
        return this[this.length - 1 - n];
};
/* Used like the [] operator, but also works with negative numbers */
Array.prototype.nth = function(n) {
        if (n === undefined) {
                n = 0;
        }
        else if (typeof n !== "number") {
                throw new Error("First argument must be a number");
        }
        if (n < 0) {
                return this.last(Math.abs(n) - 1);
        }
        else {
                return this[n];
        }
};
[1, 2, 3].last();     // 3
[1, 2, 3].last(0);    // 3
[1, 2, 3].last(1);    // 2
[1, 2, 3].last(2);    // 1
[1, 2, 3].last(3);    // undefined
[1, 2, 3].last(-1);   // 1
[1, 2, 3].last(-2);   // 2
[1, 2, 3].last(-3);   // 3
[1, 2, 3].nth();      // 1
[1, 2, 3].nth(0);     // 1
[1, 2, 3].nth(1);     // 2
[1, 2, 3].nth(-1);    // 3
[1, 2, 3].nth(-4);    // undefined
[1, 2, 3].nth(null);  // error
[1, 2, 3].last(null); // error
```

On Samstag, 23. Januar 2016 16:51:21 CET Thomas wrote:

> Is this what you're thinking?
>
> Array.prototype.nth = function (n){
>   if(n < 0){
>     return this[this.length -n];
>   } else {
>     return this[n];
>   }
> }
>
> Thomas Foster
>
> @thomasfoster96
> Ph: +61477808008
> http://thomasfoster.co/
>
> > On 23 Jan 2016, at 4:40 PM, kdex <[hidden email]> wrote:
> >
> > While John's solution doesn't run into conflicts with downward
> > compatibility, it still wouldn't solve the problem of getting the n-th
> > last element of an array. To solve this, it'd probably be a good idea to
> > extend the prototype and specify a parameter, defaulting to 1.
> >
> > `Array.prototype.last` doesn't show up a terrible lot on search engines,
> > either, so we might actually be lucky here. Other than that, I also found
> > two more threads[1][2] on the EcmaScript discussion archives that propose
> > it.
> >
> > They might be worth a read.
> >
> > [1] https://esdiscuss.org/topic/array-prototype-last
> > [2] https://esdiscuss.org/topic/proposal-array-prototype-last
> >
> >> On Samstag, 23. Januar 2016 15:44:19 CET John Gardner wrote:
> >> Using a well-known symbol to access an array's last element is probably
> >> wiser than a typical method or property:
> >>
> >> let a = [0, 1, 2, 3];
> >> console.log(
> >> a[Symbol.last] === 3
> >> /* true */
> >> );
> >>
> >> There're obviously instances where authors have extended Array prototypes
> >> with "last" methods or properties, but we can't guarantee they'd all work
> >> the same. For instance, assume there are some implementations that skip
> >> undefined values:
> >>
> >> var a = [0, 1, 2, undefined, undefined];
> >> Array.prototype.last = function(){
> >> return this[this.length - 1];
> >> };
> >> /** One that skips undefined values */
> >> Array.prototype.last = function(){
> >> var offset = 1;
> >> while(offset < this.length && undefined === this[this.length - offset])
> >> ++offset;
> >> return this[this.length - offset];
> >> }
> >>
> >> These discrepancies are subtle, but have the potential to break backwards
> >> compatibility.
> >>
> >> Using a well-known symbol eliminates the potential for conflict.
> >> Furthermore, it also offers an opportunity to complement any iterable
> >> object's ability to synthesise array-like behaviour. For instance, it
> >> enables iterables to also return the last object in their list of values:
> >>
> >> let pruebas = {
> >> data: ["Probando", "la", "mierda", "esta", undefined],
> >>
> >> [Symbol.iterator](){
> >> /* Stuff with .data */
> >> },
> >> [Symbol.last](){
> >> /** Stuff to skip undefined values or whatever */
> >> let offset = 1;
> >> let data   = this.data;
> >> while(
> >> offset < data.length &&
> >> undefined === data[data.length - offset]
> >> )
> >> ++offset;
> >> return data[data.length - offset];
> >> }
> >> }
> >>
> >> O


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

Re: JavaScript Language feature Idea

Bob Myers
On Sat, Jan 23, 2016 at 12:54 PM, kdex <[hidden email]> wrote:
[1, 2, 3].last();     // 3

I'm wondering what the ultimate motivation for suggestions like this is. Is it to save key strokes? Allow more semantic coding? Support new paradigms? Performance? 'm sure someone has already come up with a good categorization like this, would someone mind providing a link? Could one of these be considered the "theme" for the next version?

I have to admit to be being quite negative about proposals of the form "Hey, this other language does X, can we do that too?", or "It would be so cool if we could do Y", especially when these are mostly about syntax. Is a missing `last` really one of our painpoints? 

Bob





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

Re: JavaScript Language feature Idea

kdex
Not every feature addition is due to performance or paradigms. Just have a
look at ES2015: I'm sure that this has neither been the motivation for
`String.prototype.startsWith`nor for `String.prototype.includes`. Even
`String.prototype.repeat` appears so simple that a loop paired with a
concatenation could have become a popular alternative.

Of course you could solve most of these string problems with earlier versions
of the language, too, often explicitly thinking with incides. But on the
downside, your code suddenly becomes a potentially unintuitive, index-ridden
mess, introducing off-by-one and out-of-bound errors (it even happened to
someone on this thread, too, if you review Thomas' code from above). This
isn't really all too much about saving keystrokes, but mainly about writing
clean, readable and maintainable code.

There's array::back in C++, negative indices in Python as well as Bash or end
in PHP, so I don't see any reason why we should complicate things for people
coming from these languages. Nor to I see why we should torture ourselves
thinking about how the underlying data structure stores its elements
internally when all I care about is reading the last element.

Just ask yourself: Do you think it's substantially more readable to write

```js
[1, 2, 3].slice(-2)[1];
```
over
```
[1, 2, 3].last(1);
```
?

If it comes to write access,  I agree that`Symbol.last` could be another handy
addition (it doesn't have to be an "either/or" discussion, really):

[1, 2, 3][Symbol.last]; // 3
[1, 2, 3][Symbol.last] = 4; // 4
[1, 2, 3].last(1); // 2

On Samstag, 23. Januar 2016 18:01:24 CET Bob Myers wrote:

> On Sat, Jan 23, 2016 at 12:54 PM, kdex <[hidden email]> wrote:
> > [1, 2, 3].last();     // 3
>
> I'm wondering what the ultimate motivation for suggestions like this is. Is
> it to save key strokes? Allow more semantic coding? Support new paradigms?
> Performance? 'm sure someone has already come up with a good categorization
> like this, would someone mind providing a link? Could one of these be
> considered the "theme" for the next version?
>
> I have to admit to be being quite negative about proposals of the form
> "Hey, this other language does X, can we do that too?", or "It would be so
> cool if we could do Y", especially when these are mostly about syntax. Is a
> missing `last` really one of our painpoints?
>
> Bob

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

Re: JavaScript Language feature Idea

Michał Wadas
I can't consider `.last(1)` method to be readable...
But I think `.nth(n)` method with support for negative index would be really useful.

2016-01-23 17:32 GMT+01:00 kdex <[hidden email]>:
Not every feature addition is due to performance or paradigms. Just have a
look at ES2015: I'm sure that this has neither been the motivation for
`String.prototype.startsWith`nor for `String.prototype.includes`. Even
`String.prototype.repeat` appears so simple that a loop paired with a
concatenation could have become a popular alternative.

Of course you could solve most of these string problems with earlier versions
of the language, too, often explicitly thinking with incides. But on the
downside, your code suddenly becomes a potentially unintuitive, index-ridden
mess, introducing off-by-one and out-of-bound errors (it even happened to
someone on this thread, too, if you review Thomas' code from above). This
isn't really all too much about saving keystrokes, but mainly about writing
clean, readable and maintainable code.

There's array::back in C++, negative indices in Python as well as Bash or end
in PHP, so I don't see any reason why we should complicate things for people
coming from these languages. Nor to I see why we should torture ourselves
thinking about how the underlying data structure stores its elements
internally when all I care about is reading the last element.

Just ask yourself: Do you think it's substantially more readable to write

```js
[1, 2, 3].slice(-2)[1];
```
over
```
[1, 2, 3].last(1);
```
?

If it comes to write access,  I agree that`Symbol.last` could be another handy
addition (it doesn't have to be an "either/or" discussion, really):

[1, 2, 3][Symbol.last]; // 3
[1, 2, 3][Symbol.last] = 4; // 4
[1, 2, 3].last(1); // 2

On Samstag, 23. Januar 2016 18:01:24 CET Bob Myers wrote:
> On Sat, Jan 23, 2016 at 12:54 PM, kdex <[hidden email]> wrote:
> > [1, 2, 3].last();     // 3
>
> I'm wondering what the ultimate motivation for suggestions like this is. Is
> it to save key strokes? Allow more semantic coding? Support new paradigms?
> Performance? 'm sure someone has already come up with a good categorization
> like this, would someone mind providing a link? Could one of these be
> considered the "theme" for the next version?
>
> I have to admit to be being quite negative about proposals of the form
> "Hey, this other language does X, can we do that too?", or "It would be so
> cool if we could do Y", especially when these are mostly about syntax. Is a
> missing `last` really one of our painpoints?
>
> Bob

_______________________________________________
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: JavaScript Language feature Idea

kdex
In reply to this post by kdex
Just a short note:

> I was not aware that it was an explicit design goal to simplify things for
> people coming from other languages.

Well, it most certainly was. That's why we're "blessed" with ASI. :)

On Samstag, 23. Januar 2016 23:41:45 CET you wrote:

> I can't remember the last time I wanted to get the next-to-last element of
> an array. If I did, I would write `arr[arr.length-2]` and be done with it.
> If you want to methodize this as `Array.prototype.last` feel free; who's
> stopping you?
>
> I was not aware that it was an explicit design goal to simplify things for
> people coming from other languages. In some cases, things like array
> slicing was built into these languages in extremely clean fashion from the
> start. Trying to splice equivalent syntaxes onto JS is no trivial matter in
> many cases.
>
> Is this supposed to be the same as `push`?
>
> --
> Bob
>
> On Sat, Jan 23, 2016 at 10:02 PM, kdex <[hidden email]> wrote:
> > Not every feature addition is due to performance or paradigms. Just have a
> > look at ES2015: I'm sure that this has neither been the motivation for
> > `String.prototype.startsWith`nor for `String.prototype.includes`. Even
> > `String.prototype.repeat` appears so simple that a loop paired with a
> > concatenation could have become a popular alternative.
> >
> > Of course you could solve most of these string problems with earlier
> > versions
> > of the language, too, often explicitly thinking with incides. But on the
> > downside, your code suddenly becomes a potentially unintuitive,
> > index-ridden
> > mess, introducing off-by-one and out-of-bound errors (it even happened to
> > someone on this thread, too, if you review Thomas' code from above). This
> > isn't really all too much about saving keystrokes, but mainly about
> > writing
> > clean, readable and maintainable code.
> >
> > There's array::back in C++, negative indices in Python as well as Bash or
> > end
> > in PHP, so I don't see any reason why we should complicate things for
> > people
> > coming from these languages. Nor to I see why we should torture ourselves
> > thinking about how the underlying data structure stores its elements
> > internally when all I care about is reading the last element.
> >
> > Just ask yourself: Do you think it's substantially more readable to write
> >
> > ```js
> > [1, 2, 3].slice(-2)[1];
> > ```
> > over
> > ```
> > [1, 2, 3].last(1);
> > ```
> > ?
> >
> > If it comes to write access,  I agree that`Symbol.last` could be another
> > handy
> > addition (it doesn't have to be an "either/or" discussion, really):
> >
> > [1, 2, 3][Symbol.last]; // 3
> > [1, 2, 3][Symbol.last] = 4; // 4
> > [1, 2, 3].last(1); // 2
> >
> > On Samstag, 23. Januar 2016 18:01:24 CET Bob Myers wrote:
> > > On Sat, Jan 23, 2016 at 12:54 PM, kdex <[hidden email]> wrote:
> > > > [1, 2, 3].last();     // 3
> > >
> > > I'm wondering what the ultimate motivation for suggestions like this is.
> >
> > Is
> >
> > > it to save key strokes? Allow more semantic coding? Support new
> >
> > paradigms?
> >
> > > Performance? 'm sure someone has already come up with a good
> >
> > categorization
> >
> > > like this, would someone mind providing a link? Could one of these be
> > > considered the "theme" for the next version?
> > >
> > > I have to admit to be being quite negative about proposals of the form
> > > "Hey, this other language does X, can we do that too?", or "It would be
> >
> > so
> >
> > > cool if we could do Y", especially when these are mostly about syntax.
> >
> > Is a
> >
> > > missing `last` really one of our painpoints?
> > >
> > > Bob


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