New Array Functions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

New Array Functions

Sebastian Malton
Using the ~ to count from the back of the array.

Basically 

```js
let x = [1,2,3,4,5];
x[4] === x[~0] === 5; //I know this is not how JS checking works
```

Sebastian 

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

Re: New Array Functions

T.J. Crowder-2
`~` has a meaning: http://www.ecma-international.org/ecma-262/8.0/index.html#sec-bitwise-not-operator `~0` is `-1`. `x[-1]` is accessing the property called `"-1"` in the array object. (Which it probably doesn't have, but certainly *could* have.)

If you want to propose something else, you'll need to provide some means of differentiating from existing syntax, since of course breaking existing syntax is not going to happen.

-- T.J. Crowder

On Tue, Jul 11, 2017 at 6:19 PM, Sebastian Malton <[hidden email]> wrote:
Using the ~ to count from the back of the array.

Basically 

```js
let x = [1,2,3,4,5];
x[4] === x[~0] === 5; //I know this is not how JS checking works
```

Sebastian 

_______________________________________________
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
|  
Report Content as Inappropriate

Re: New Array Functions

Guylian Cox
In reply to this post by Sebastian Malton
Unfortunately, what you're proposing is already valid and has a different behavior:

```javascript
> x = [1, 2, 3, 4, 5];
[ 1, 2, 3, 4, 5 ]
> x[-1] = 'oh no'
'oh no'
> x[~0]
'oh no'
> x[4]
5
```

What about adding a prototype method like `last` to Array instead? With `[1,2,3].last()` returning 3, `[1,2,3].last(1)` returning 2, etc

Le mar. 11 juil. 2017 à 19:19, Sebastian Malton <[hidden email]> a écrit :
Using the ~ to count from the back of the array.

Basically 

```js
let x = [1,2,3,4,5];
x[4] === x[~0] === 5; //I know this is not how JS checking works
```

Sebastian 
_______________________________________________
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
|  
Report Content as Inappropriate

Re: New Array Functions

Alexander Jones
`array.get(-1)` could have the desired semantics (and be nicely symmetric with Map), but actually in other languages (e.g. Python) I find that this "trick" with negative numbers can lead to a surprising (and scary) propagation of bugs, where array bounds checking is not correctly done. Actually this exact behaviour caught me^Wsomeone who isn't me out, in Immutable.js's `List` type already.

On 11 July 2017 at 18:31, Guylian Cox <[hidden email]> wrote:
Unfortunately, what you're proposing is already valid and has a different behavior:

```javascript
> x = [1, 2, 3, 4, 5];
[ 1, 2, 3, 4, 5 ]
> x[-1] = 'oh no'
'oh no'
> x[~0]
'oh no'
> x[4]
5
```

What about adding a prototype method like `last` to Array instead? With `[1,2,3].last()` returning 3, `[1,2,3].last(1)` returning 2, etc

Le mar. 11 juil. 2017 à 19:19, Sebastian Malton <[hidden email]> a écrit :
Using the ~ to count from the back of the array.

Basically 

```js
let x = [1,2,3,4,5];
x[4] === x[~0] === 5; //I know this is not how JS checking works
```

Sebastian 
_______________________________________________
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
Loading...