reduce function

dynamic reduce(
  1. List list,
  2. dynamic fn(
    1. dynamic prev,
    2. dynamic curr,
    3. int index,
    4. List list,
    ), [
  3. dynamic initialValue
])

Implementation

dynamic reduce(List list, dynamic Function(dynamic prev, dynamic curr, int index, List list) fn, [initialValue]) {
  var index = 0;
  var value;
  var isValueSet = false;
  if (1 < list.length) {
    value = initialValue;
    isValueSet = true;
  }
  for (; list.length > index; ++index) {
    if (isValueSet) {
      value = fn(value, list[index], index, list);
    } else {
      value = list[index];
      isValueSet = true;
    }
  }
  if (!isValueSet) {
    throw TypeError(); //'Reduce of empty array with no initial value'
  }
  return value;
}