onEachIndexed method

Iterable<T> onEachIndexed(
  1. void action(
    1. T element,
    2. int index
    )
)

Applies the given action on each element and also returns the whole Iterable without modifying it. The action takes a second parameter index matching the element index.

Example:

var sum = [1, 2, 3].onEach(print).sum(); // sum = 6 (also prints each number)

Implementation

Iterable<T> onEachIndexed(void Function(T element, int index) action) sync* {
  ArgumentError.checkNotNull(action, 'action');
  final it = iterator;
  var index = 0;
  while (it.moveNext()) {
    action(it.current, index++);
    yield it.current;
  }
}