Accumulator<T, U> typedef

Accumulator<T, U> = U Function({U? previous, required T value})

A function that takes a newly parsed argument value value and a potentially existing accumulated value previous and returns a new accumulated value. In other words, this is very similar to Iterable.reduce, but designed for Plade.

// An accumulator that sums all the given arguments.
Accumulator<int, int> sumAccumulator =
    (i, sum) => (sum ?? 0) + i;
// An accumulator that counts how many times an argument is given the same
// value.
Accumulator<String, Map<String, int>> freqAccumulator =
    (arg, freqs) => (freqs ?? <String, int>{})
        ..update(arg, (v) => v + 1, ifAbsent: () => 1);

Implementation

typedef Accumulator<T, U> = U Function({required T value, U? previous});