Fold<T, S> typedef

Fold<T, S> = S Function(S currentSummary, T newValue)

A function that takes a summary and a new value and returns a new summary updated with the new value.

Examples:

  • Update a list by appending strings to it: (lst, newString) => List.from(lst)..add(newString);
  • Update a running average of integers:
      final double? average;
      final int n;
    
      RunningAverage({this.average : 0, required this.n});
    
      RunningAverage updated(int newVal) =>
          RunningAverage(average: ((average ?? 0)*n + newVal) / (n + 1), n: n + 1);
    }
    .
    .
    .
    (avg, newVal) => avg.updated(newVal);
    

Implementation

typedef Fold<T, S> = S Function(S currentSummary, T newValue);