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

slice(-10, -2) === slice(0, 8) #25

Closed
xgbuils opened this issue Jun 22, 2017 · 6 comments
Closed

slice(-10, -2) === slice(0, 8) #25

xgbuils opened this issue Jun 22, 2017 · 6 comments

Comments

@xgbuils
Copy link

xgbuils commented Jun 22, 2017

I analyzed the code for slice implementation and it seems that slice(-10, -2) behaves equal than slice(0, 8). Is the expected behaviour?

@benji6
Copy link
Owner

benji6 commented Jun 24, 2017

Yeah I didn't really give any thought to negative arguments and haven't said anything about that in the docs. I see you handle it like this https://github.com/xgbuils/iterarray/blob/master/src/index.js#L39 I'm thinking if any of the arguments are negative I'll just handle it like native slice - possibly by just spreading into an array and dispatching to native slice because that's the easiest way to do it! :D I might submit a PR on this soon

@xgbuils
Copy link
Author

xgbuils commented Jun 24, 2017

I like your approach following the native slice behaviour. But it's a bit eager for infinite iterables.

@xgbuils
Copy link
Author

xgbuils commented Jun 24, 2017

And if you want to assume this eager behaviour, I suggest to put if (n >= m) return module.exports.empty() sentence first to avoid some cases that is no way necessary to resolve eagerly.

@benji6
Copy link
Owner

benji6 commented Jun 24, 2017

That then breaks the behavior with negative indices: slice(0, -1, range(1,3)) should be (1 2) not (). That's why I put the if (n >= m) return module.exports.empty() second. To mimic native slice it has to be eager because the offset is done from the end of the iterable so I think this is fine

@xgbuils
Copy link
Author

xgbuils commented Jun 24, 2017

It's true. When I said that, I was thinking on n < 0 && m < 0 case. In this case if (n >= m) return module.exports.empty() sentence is valid. I mean slice(-2, -5, iterable) always returns empty iterable regardless if iterable is infinite or not. But in cases that one parameter is negative and the other not, it seems that eager solution is the best solution.

@benji6
Copy link
Owner

benji6 commented Jun 24, 2017

Ah yeah, very good point then! I'll add a test case, make the change then merge this

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

No branches or pull requests

2 participants