fold<T> method

T fold<T>(
  1. T initialValue,
  2. T combine(
    1. T previousValue,
    2. MapEntry<K, V> element
    )
)

Reduces the Map to a single value by iteratively combining combine each key/value pair entry in the Map with an existing value initialValue.

Uses initialValue as the initial value, then iterates through the key/value pairs and updates the value with the result of the combine function.

Implementation

T fold<T>(
  T initialValue,
  T Function(T previousValue, MapEntry<K, V> element) combine,
) {
  var result = initialValue;
  forEach((k, v) => result = combine(result, MapEntry(k, v)));
  return result;
}