-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add _.slice function #1758
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
Add _.slice function #1758
Conversation
|
As long as the
???
The |
|
@megawac: please rebase |
|
Rebased |
|
👍 |
|
Can you explain why and when you'd want to use this instead of vanilla slice? |
|
The primary reason for me was the vanilla slice can return sparse arrays, the util version will return dense (which is what other Underscore methods return). There's a bonus of small/medium collections being sliced faster, supporting nodelists in IE < 9, and Graeme's addition of the |
|
Are there any use cases for |
|
I think this is trying to do too much. Can we make it a simple, dense version of |
|
@jashkenas from my experience, the step arg is most commonly used for reversing but it can also be useful for picking every 2nd element. I'm not sure if thats useful enough for us to entertain though (was fun to implement though) |
|
Not all review comments were addressed. |
Add a slice function with the python contract
_.slice(collection, start, end, step)See https://docs.python.org/2/library/functions.html#slice. Like python slice, it doesn't do any coercing of inputs (besides 0 which is coerced to 1). Python will give an error for invalid inputs, this implementation doesn't bother and carries on. One nice benefit of the step feature is now we can do non destructive reversing
_.slice(arr, null, null, -1)Created some jsperf benchmarks the other day http://jsperf.com/cr-52768
I think it may make sense to add a
_.reversefunction after this which will also fix a bug_(document.childNodes).reverse(); // Exception