spider icon indicating copy to clipboard operation
spider copied to clipboard

more powerful array splicing:

Open nmn opened this issue 11 years ago • 4 comments

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

nmn avatar Dec 01 '14 20:12 nmn

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

CryZe avatar Dec 01 '14 20:12 CryZe

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

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

alongubkin avatar Dec 01 '14 20:12 alongubkin

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.

Namek avatar Dec 01 '14 21:12 Namek

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];

nmn avatar Dec 01 '14 22:12 nmn