Reference for stage 3 iterator-chunking - #44884
Conversation
|
Preview URLs (3 pages)
External URLs (4)URL:
URL:
(comment last updated: 2026-07-22 12:26:43) |
084b4d4 to
bad53e5
Compare
| sidebar: jsref | ||
| --- | ||
|
|
||
| The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk. |
There was a problem hiding this comment.
There is something not quite right about "Each time" - i.e. it isn't clear that the thing being iterated here is the helper object. You could do
| The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk. | |
| The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields non-overlapping element sequences as arrays. Each time the helper is iterated, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk. |
There was a problem hiding this comment.
I also found the difference between non-overlapping an overlapping methods non-intuitive. I prefer this:
| The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk. | |
| The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that splits the elements from the original iterator into consecutive array chunks. Each time the helper is iterated, it gets the specified number of elements from the underlying iterator and yields them together. |
|
|
||
| ### Return value | ||
|
|
||
| A new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects). Each time the returned iterator helper's `next()` method is called, the current iterator is advanced by `chunkSize` elements, and those elements are yielded together as an array. |
There was a problem hiding this comment.
| A new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects). Each time the returned iterator helper's `next()` method is called, the current iterator is advanced by `chunkSize` elements, and those elements are yielded together as an array. | |
| A new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects). Each time the returned iterator helper's `next()` method is called, the original iterator is advanced by `chunkSize` elements, and those elements are yielded together as an array. |
|
|
||
| A new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects). Each time the returned iterator helper's `next()` method is called, the current iterator is advanced by `chunkSize` elements, and those elements are yielded together as an array. | ||
|
|
||
| If the current iterator has some but fewer than `chunkSize` elements remaining, those elements are still yielded as an array (so the length is less than `chunkSize`), and the iterator helper will be immediately completed the next time `next()` is called. |
There was a problem hiding this comment.
| If the current iterator has some but fewer than `chunkSize` elements remaining, those elements are still yielded as an array (so the length is less than `chunkSize`), and the iterator helper will be immediately completed the next time `next()` is called. | |
| If the original iterator has some but fewer than `chunkSize` elements remaining, those elements are still yielded as an array (so the length is less than `chunkSize`), and the iterator helper will be immediately completed the next time `next()` is called. |
|
|
||
| If the current iterator has some but fewer than `chunkSize` elements remaining, those elements are still yielded as an array (so the length is less than `chunkSize`), and the iterator helper will be immediately completed the next time `next()` is called. | ||
|
|
||
| If the current iterator has no elements remaining, the iterator helper is immediately completed without yielding an empty array. |
There was a problem hiding this comment.
| If the current iterator has no elements remaining, the iterator helper is immediately completed without yielding an empty array. | |
| If the original iterator has no elements remaining, the iterator helper is immediately completed without yielding an empty array. |
| The **`windows()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields overlapping element sequences as arrays. A sliding window sweeps across the input iterator; each time, a new element is added to the right while the leftmost element is removed. | ||
|
|
||
| For yielding non-overlapping sequences (i.e., chunking), see {{jsxref("Iterator.prototype.chunks()")}}. |
There was a problem hiding this comment.
Same problem with my intuition in this case. I think this might be easier for most people to parse.
| The **`windows()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields overlapping element sequences as arrays. A sliding window sweeps across the input iterator; each time, a new element is added to the right while the leftmost element is removed. | |
| For yielding non-overlapping sequences (i.e., chunking), see {{jsxref("Iterator.prototype.chunks()")}}. | |
| The **`windows()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields a sliding window of elements as an array. On each iteration, the window slides forward by one element — yielding an array that drops the first element from the previous iteration and adds the next element from the original iterator. | |
| To instead split elements into distinct chunks, see {{jsxref("Iterator.prototype.chunks()")}}. |
| ## Instance methods | ||
|
|
||
| - {{jsxref("Iterator.prototype.chunks()")}} | ||
| - : Returns a new iterator helper object that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk. |
There was a problem hiding this comment.
Same comment about "each time" and overlapping vs non-overlapping as for the method docs.
Part of #44846