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/pending/proposal-iterator-chunking.md.
  • 简体中文
  • iterator chunking S3

    中文标题:迭代器分块

    提案概览
    提案速览

    该提案为迭代器添加了 chunks()windows() 方法,用于按给定大小消费非重叠和重叠的子序列。它涵盖了广泛的使用场景,如分页、批处理和滑动窗口计算。

    Note

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

    迭代器分块

    一个 TC39 提案,用于将迭代器按可配置的大小消费为重叠或非重叠的子序列。

    阶段: 3。进一步推进取决于 2 个或更多已实现的实现。

    规范: https://tc39.es/proposal-iterator-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] ]
    滑动窗口的用例
    • 运行/连续计算,如平均值
    • 上下文敏感算法,如成对比较
    • 轮播及其类似物(当应用于无限循环时)

    先例

    其他语言

    语言chunkswindows大小为0的chunks?截断windows?
    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恐慌否,步长不可配置
    Rustslicechunkswindows恐慌否,步长不可配置
    ScalaSeqgroupedsliding抛出异常
    SwiftSequence----不适用不适用

    JS库

    chunkswindows大小为0的chunks?截断windows?
    chunkchunk--将0强制转换为false 😞不适用
    extra-iterablechunkchunk无限空数组
    iter-opspage--抛出异常不适用
    iter-toolsbatchwindow, windowAhead, windowBehind抛出异常可选
    iterablefuchunk--将所有内容收集到单个数组不适用
    itertools-tschunkwisechunkwiseOverlap抛出异常
    Lodash / Underscorechunk--无限空数组不适用
    RamdasplitEveryaperture无限空数组
    sequencychunk--抛出异常不适用
    wuchunk--将所有内容收集到单个数组不适用