alongubkin / spider

Unsurprising JavaScript - No longer active
http://spiderlang.org
Apache License 2.0
1.34k stars 46 forks source link

more powerful array splicing: #112

Open nmn opened 9 years ago

nmn commented 9 years ago

Spider already supports:

numbers[3..6] = [-3, -4, -5, -6];

This should work even if the number of elements on the right don't watch. For example:

var arr = [0,1,2,3,4,5,6];
/// arr.length === 7;

arr[2...4] = [-2];
// arr === [0, 1, -2, 5, 6]
// arr.length === 5
CryZe commented 9 years ago

I would've expected this to just set those elements to -2, not actually remove them.

alongubkin commented 9 years ago

@CryZe I would've expected this to set those elements to -2 if the -2 wasn't in an array:

arr[2...4] = -2;
Namek commented 9 years ago

There's a difference between:

arr[2...4] = 0

and

arr[2...4] = [0];

that way both @nmn and @CryZe would be happy. @alongubkin I'm sorry, I don't feel your way in there.

nmn commented 9 years ago

Well, my proposal was straight from Swift. To be fair, the existing range splicing looked to be straight out of swift, so it felt completely obvious to me.

In swift:

var arr = [0,1,2,3,4,5,6,7,8,9]
arr[2...8] = [99]
// arr = [0, 1, 99, 9]

Also, you can shove in a bigger array instead:

var arr = [0, 1, 9];
arr[1..<2] = [1,2,3,4,5,6,7,8];
// arr =  [0,1,2,3,4,5,6,7,8,9];

In Swift: arr[2...5] = -2 throws an error.

This whole thing feels natural to me and kind of similar to spread operators in ES6 Arrays:

var arr1 = [2,3,4,5];
var arr2 = [0, 1, ...arr1, 6,7,8,9];
// arr =  [0,1,2,3,4,5,6,7,8,9];