combineIterables<A, B, R> function

Iterable<R> combineIterables<A, B, R>(
  1. Iterable<A> a,
  2. Iterable<B> b,
  3. R combine(
    1. A,
    2. B
    ), {
  4. bool allowDifferentSizes = false,
})

Combines iterables a and b into one, by applying the combine function. If allowDifferentSizes is true, it will stop as soon as one of the iterables has no more values. If allowDifferentSizes is false, it will throw an error if the iterables have different length.

See also: IterableZip

Implementation

Iterable<R> combineIterables<A, B, R>(
  Iterable<A> a,
  Iterable<B> b,
  R Function(A, B) combine, {
  bool allowDifferentSizes = false,
}) sync* {
  Iterator<A> iterA = a.iterator;
  Iterator<B> iterB = b.iterator;

  while (iterA.moveNext()) {
    if (!iterB.moveNext()) {
      if (allowDifferentSizes)
        return;
      else
        throw StateError("Can't combine iterables of different sizes (a > b).");
    }
    yield combine(iterA.current, iterB.current);
  }

  if (iterB.moveNext() && !allowDifferentSizes)
    throw StateError("Can't combine iterables of different sizes (a < b).");
}