Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more powerful array splicing: #112

Open
nmn opened this issue Dec 1, 2014 · 4 comments
Open

more powerful array splicing: #112

nmn opened this issue Dec 1, 2014 · 4 comments
Labels

Comments

@nmn
Copy link

nmn commented Dec 1, 2014

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
Copy link

CryZe commented Dec 1, 2014

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

@alongubkin
Copy link
Owner

@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
Copy link
Collaborator

Namek commented Dec 1, 2014

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
Copy link
Author

nmn commented Dec 1, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants