zip<R, V> method

Iterable<V> zip<R, V>(
  1. Iterable<R> other,
  2. V transform(
    1. E a,
    2. R b
    )
)

Returns a new lazy Iterable of values built from the elements of this collection and the other collection with the same index.

Using the provided transform function applied to each pair of elements. The returned list has length of the shortest collection.

Example (with added type definitions for transform parameters):

final amounts = [2, 3, 4];
final animals = ['dogs', 'birds', 'cats'];
final all = amounts.zip(
 animals,
 (int amount, String animal) => '$amount $animal'
);  // returns: ['2 dogs', '3 birds', '4 cats']

Implementation

Iterable<V> zip<R, V>(
  Iterable<R> other,
  V Function(E a, R b) transform,
) sync* {
  final it1 = iterator;
  final it2 = other.iterator;
  while (it1.moveNext() && it2.moveNext()) {
    yield transform(it1.current, it2.current);
  }
}