proposal-deiter icon indicating copy to clipboard operation
proposal-deiter copied to clipboard

take/takeLast could also return double-ended iterators

Open hax opened this issue 3 years ago • 1 comments

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 https://github.com/tc39/proposal-iterator-helpers/issues/81#issue-597636898 , so invoking all these four methods on deiters always return deiters.

hax avatar Jul 04 '22 12:07 hax

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.

hax avatar Jul 04 '22 13:07 hax