benji6/imlazy

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

xgbuils opened this issue · 6 comments

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

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

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

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.

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

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.

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