Home

Awesome

Joint Iteration

A TC39 proposal to synchronise the advancement of multiple iterators.

Stage: 2.7

Demo: https://tc39.es/proposal-joint-iteration/demo/

Specification: https://tc39.es/proposal-joint-iteration/

motivation

Often you have 2 or more iterators that are positionally aligned (the first value yielded by the first iterator corresponds to the first value yielded by the other iterators, and so on), and you would like to operate on the corresponding values together. A common solution to this is zip, which produces an iterator of the combined values. zipWith allows combination of values in some way other than tupling. Some languages express zipWith as a variadic map.

presentations to committee

considered design space

  1. do we support just 2 iterators or something else? 2+? 1+? 0+?
    1. if 0 is allowed, is that considered never-ending or already completed?
    2. should the iterators be passed positionally (combining to arrays) or named (combining to objects)?
    3. do we take the iterators as varargs or as an iterable/object?
      1. varargs eliminates design space for potentially passing an options bag or a combining function
  2. do we support iterators and iterables like Iterator.from and flatMap?
    1. if so, which string handling do we match? Iterator.from iterates strings; flatMap rejects strings
  3. if an iterator completes, do we still advance the other iterators?
    1. do we return them?
  4. if an iterator fails to advance, do we still advance the other iterators?
    1. if so, do we return an AggregateError? Only if 2+ failures?
  5. do we want -With variants for combining the values in other ways than tupling?
    1. what about always requiring the combiner?
  6. do we want a zipLongest/zipFilled/zipAll?
    1. if so, do we want a filler element or to call a function to provide the filler?
    2. what about a variant that matches the length of a privileged iterator (this)?
  7. do we want a zipEqual/zipStrict that throws if they do not complete after the same number of yields?

prior art

other languages

languageshortestlongestprivilegedstrict-With3+ sources1 source0 sources
C++std::ranges::views::zip::zip_transformyesyes
Clojurevariadic mapyesyesyes
ElmList.map2yesyes
HaskellzipzipWithyes
OCamlzipcombinemap2yes
Pythonzipitertools.zip_longestzip(..., strict=True)yesyesyes, empty
RubyEnumerable#zipzipyesyes
RustIterator::zip
Scalazipit.zipAll(jt, x, y)
Swiftzip

JS libraries

libraryshortestlongestprivilegedstrict-With3+ sources1 source0 sources
@iterable-iterator/zipzipzipLongestyesyes
@softwareventures/iteratorzipOnce
extra-iterablezipzipzipyesyesyes, empty
immutable.jsSeq::zipzipWithyesyes
iter-opszipyesyesyes, empty
iter-toolszipzipAllyesyesyes, empty
iterablefuzipzipAllyesyesyes, empty
iterarezip
itertools-tszipzipFilled, zipLongestzipEqualyesyesyes, empty
ixjszipyesyesyes, empty
lodashzipzipWithyesyesyes, empty
ramdazipzipWith
sequencyzip
wuzipzipLongestzipWithyesyes
zipiteratorszipiterators