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

take/takeLast could also return double-ended iterators #13

Open
hax opened this issue Jul 4, 2022 · 1 comment
Open

take/takeLast could also return double-ended iterators #13

hax opened this issue Jul 4, 2022 · 1 comment

Comments

@hax
Copy link
Member

hax commented Jul 4, 2022

Double-ended iterators could support takeLast/dropLast methods. The result iterator of drop/dropLast could still be double-ended, because drop/dropLast just skip some items. But take/takeLast are different. Normally, the result of take() can only call next(), similarly, the result of takeLast() can only call nextLast(). So deiter.takeLast(1000).filter(x => x > 0).take(10) won't work (throw TypeError). deiter.takeLast(1000).filter(x => x > 0).toArray().values().take(10) could work, but require convert to array is inconvenient, and waste memory (in the example, we only need 10 items, but we create an 1000 items array).

Consider take/drop/takeLast/dropLast normally only have a relative small n, a better solution is using buffer based semantic like tc39/proposal-iterator-helpers#81 (comment) , so invoking all these four methods on deiters always return deiters.

@hax
Copy link
Member Author

hax commented Jul 4, 2022

One problem is how the biggest limit we support ? Consider the largest array length is 2**32-1, it seems this should be the max, or could it be even a small number, like 10000? If larger than the max, just throw? or some other behavior?

A special case is limit == Infinity, in that case, take/takeLast should return a simple wrapper, no need to buffer, always deiter.

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

1 participant