Stage 2 Draft / July 1, 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.sliding ( windowSize )

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. Set iterated to ? GetIteratorDirect(O).
  6. Let closure be a new Abstract Closure with no parameters that captures iterated and windowSize 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 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).
  7. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, ยซ [[UnderlyingIterators]] ยป).
  8. Set result.[[UnderlyingIterators]] to ยซ iterated ยป.
  9. Return result.

3 Iterator.prototype.windows ( windowSize )

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. Set iterated to ? GetIteratorDirect(O).
  6. Let closure be a new Abstract Closure with no parameters that captures iterated and windowSize 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, 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).
  7. Let result be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, ยซ [[UnderlyingIterators]] ยป).
  8. Set result.[[UnderlyingIterators]] to ยซ iterated ยป.
  9. Return result.