≪ Today I learned.
RSS購読
    公開日
    タグ
    JavaScript
    著者
    ダーシノ

    Iterator Sequencingでイテレータを合成する

    2024年10月にIterator SequencingがStage2.7にアップデートされた。

    Iterator SequencingはArray#concat()のように複数のイテレータを合成し、新しいイテレータを返す。

    この他のIterator関連のメソッドはIterator Helpersを参照。

    Before

    const lows = Iterator.from([0, 1, 2, 3])
    const highs = Iterator.from([6, 7, 8, 9])
    
    const merged = function* () {
      yield* lows
      yield 4
      yield 5
      yield* highs
    }
    
    Array.from(merged())  // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    After

    const lows = Iterator.from([0, 1, 2, 3])
    const highs = Iterator.from([6, 7, 8, 9])
    
    const merged = Iterator.concat(
      lows,
      [4, 5],
      highs
    )