Stage 2 Draft / September 16, 2025

Iterator Chunking

1 Iterator.prototype.chunks ( chunkSize )

This method performs the following steps when called:

  1. Let O be the this value.
  2. If O is not an Object, throw a TypeError exception.
  3. Let iterated be the Iterator Record { [[Iterator]]: O, [[NextMethod]]: undefined, [[Done]]: false }.
  4. If chunkSize is not an integral Number in the inclusive interval from 1𝔽 to 𝔽(232 - 1), then
    1. Let error be ThrowCompletion(a newly created RangeError object).
    2. Return ? IteratorClose(iterated, error).
  5. Set iterated to ? GetIteratorDirect(O).
  6. Let closure be a new Abstract Closure with no parameters that captures iterated and chunkSize and performs the following steps when called:
    1. Let buffer be a new empty List.
    2. Repeat,
      1. Let value be ? IteratorStepValue(iterated).
      2. If value is done, then
        1. If buffer is not empty, then
          1. Perform Completion(Yield(CreateArrayFromList(buffer))).
        2. Return ReturnCompletion(undefined).
      3. Append value to buffer.
      4. If the number of elements in buffer is (chunkSize), then
        1. Let completion be Completion(Yield(CreateArrayFromList(buffer))).
        2. IfAbruptCloseIterator(completion, iterated).
        3. Set buffer to a new empty List.
  7. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterators]] »).
  8. Set result.[[UnderlyingIterators]] to « iterated ».
  9. Return result.

2 Iterator.prototype.windows ( windowSize [ , undersized ] )

This method performs the following steps when called:

  1. Let O be the this value.
  2. If O is not an Object, throw a TypeError exception.
  3. Let iterated be the Iterator Record { [[Iterator]]: O, [[NextMethod]]: undefined, [[Done]]: false }.
  4. If windowSize is not an integral Number in the inclusive interval from 1𝔽 to 𝔽(232 - 1), then
    1. Let error be ThrowCompletion(a newly created RangeError object).
    2. Return ? IteratorClose(iterated, error).
  5. If undersized is undefined, set undersized to "only-full".
  6. If undersized is neither "only-full" nor "allow-partial", then
    1. Let error be ThrowCompletion(a newly created TypeError object).
    2. Return ? IteratorClose(iterated, error).
  7. Set iterated to ? GetIteratorDirect(O).
  8. Let closure be a new Abstract Closure with no parameters that captures iterated, windowSize, and undersized and performs the following steps when called:
    1. Let buffer be a new empty List.
    2. Repeat,
      1. Let value be ? IteratorStepValue(iterated).
      2. If value is done, then
        1. If undersized is "allow-partial", buffer is not empty, and the number of elements in buffer < (windowSize), then
          1. Perform Completion(Yield(CreateArrayFromList(buffer))).
        2. Return ReturnCompletion(undefined).
      3. If the number of elements in buffer is (windowSize), then
        1. Remove the first element from buffer.
      4. Append value to buffer.
      5. If the number of elements in buffer is (windowSize), then
        1. Let completion be Completion(Yield(CreateArrayFromList(buffer))).
        2. IfAbruptCloseIterator(completion, iterated).
  9. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterators]] »).
  10. Set result.[[UnderlyingIterators]] to « iterated ».
  11. Return result.