reduce method

MapEntry<K, V> reduce(
  1. MapEntry<K, V> combine(
    1. MapEntry<K, V> accumulator,
    2. MapEntry<K, V> entry
    )
)

Reduces the Map to a single key/value pair MapEntry by iteratively combining combine each entry of the Map into an accumulator.

The Map must have at least one key/value pair. If it has only one pair, that pair is returned.

Otherwise this method starts with the first pair from the Map iterator, and then combines it with the remaining pairs in iteration order.

Implementation

MapEntry<K, V> reduce(
  MapEntry<K, V> Function(MapEntry<K, V> accumulator, MapEntry<K, V> entry)
      combine,
) {
  Iterator<MapEntry<K, V>> iterator = entries.iterator;
  if (!iterator.moveNext()) throw ArgumentError('$this cannot be empty.');

  var entry = iterator.current;
  while (iterator.moveNext()) {
    entry = combine(entry, iterator.current);
  }

  return entry;
}