For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/year/2027/proposal-iterator-chunking.md.
  • 简体中文
  • iterator chunking S4

    中文标题:迭代器分块

    提案概览
    提案速览

    该提案为迭代器增加了按可配置大小分组消费序列的方法。它支持非重叠分块和重叠滑动窗口。README 展示了这些操作如何生成子序列数组,并列出了分页、流处理和连续计算等相关使用场景。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    迭代器分块

    该 TC39 提案旨在以可配置大小的重叠或非重叠子序列来消费迭代器。

    阶段: 4。进一步的推进取决于两个或更多已交付的实现。

    规范: https://tc39.es/proposal-iterator-chunking/

    提交给委员会的演示

    动机

    一次消费流中的多个值可能很有用。例如,某些算法需要查看相邻元素。

    分块

    对于非重叠子序列,通常通过“分块”(chunking)方法解决,其工作方式如下:

    const digits = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].values();
    
    let chunksOf2 = Array.from(digits().chunks(2));
    // [ [0, 1], [2, 3], [4, 5], [6, 7], [8, 9] ]
    
    let chunksOf3 = Array.from(digits().chunks(3));
    // [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [9] ]
    
    let chunksOf4 = Array.from(digits().chunks(4));
    // [ [0, 1, 2, 3], [4, 5, 6, 7], [8, 9] ]
    分块的使用场景
    • 分页
    • 列式/网格布局,例如日历
    • 批处理/流处理
    • 矩阵运算
    • 格式化/编码
    • 分桶(对于已知大小的迭代器,使用计算出的分块大小)

    滑动窗口

    当需要重叠序列时,这通常称为“滑动窗口”。

    let windowsOf2 = Array.from(digits().windows(2));
    // [ [0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9] ]
    
    let windowsOf3 = Array.from(digits().windows(3));
    // [ [0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9] ]
    
    let windowsOf4 = Array.from(digits().windows(4));
    // [ [0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7], [5, 6, 7, 8], [6, 7, 8, 9] ]
    滑动窗口的使用场景
    • 连续/持续计算,例如平均值
    • 上下文相关算法,例如成对比较
    • 轮播及其类似物(当应用于无限循环时)

    现有技术

    其他语言

    语言库分块窗口0 个元素的分块?是否截断窗口?
    C++std::ranges::viewschunkslide未定义行为否
    Clojurecorepartitionpartition无限空列表填充不足时;
    在 1 之后终止
    ElmList.ExtragroupsOfgroupsOfWithStep空列表否
    HaskellsplitchunksOfdivvy无限空列表是
    JavaStreamGatherers.windowFixedGatherers.windowSliding抛出异常否,步长不可配置
    KotlinIterablechunkedwindowed抛出异常可通过参数配置
    .NETSystem.LinqEnumerable.Chunk--抛出异常不适用
    PHParrayarray_chunk--抛出异常不适用
    Pythonitertools (3.12)batched--??不适用
    Pythonmore-itertoolsgrouperwindowed空迭代器否,必须提供填充值
    RubyEnumerableeach_sliceeach_cons抛出异常否,步长不可配置
    RustIteratorarray_chunksmap_windows触发 panic否,步长不可配置
    Rustslicechunkswindows触发 panic否,步长不可配置
    ScalaSeqgroupedsliding抛出异常是
    SwiftSequence----不适用不适用

    JS 库

    库分块窗口0 个元素的分块?是否截断窗口?
    chunkchunk--将 0 强制转换为 false 😞不适用
    extra-iterablechunkchunk无限空数组是
    iter-opspage--抛出异常不适用
    iter-toolsbatchwindow, windowAhead, windowBehind抛出异常可选
    iterablefuchunk--将所有内容收集到单个数组中不适用
    itertools-tschunkwisechunkwiseOverlap抛出异常是
    Lodash / Underscorechunk--无限空数组不适用
    RamdasplitEveryaperture无限空数组否
    sequencychunk--抛出异常不适用
    wuchunk--将所有内容收集到单个数组中不适用