reduce<K, V> static method

V Function(List<K> list) reduce<K, V>(
  1. V fn(
    1. V acc,
    2. K elem
    ),
  2. V acc
)

Reduces acc by calling fn on every element of list. fn should modify acc and then return it. Call like FP.reduce<K,V>(fn, acc)(list)

Implementation

static V Function(List<K> list) reduce<K, V>(
    V Function(V acc, K elem) fn, V acc) {
  return (final List<K> list) {
    if (list.isEmpty) {
      return acc;
    }

    acc = fn(acc, list.first);

    return reduce<K, V>(fn, acc)(list.skip(1).toList());
  };
}