fold<T> method

T fold<T>(
  1. T initial,
  2. T combine(
    1. T acc,
    2. LatexNode node
    )
)

Depth-first fold: Applies combine to itself and all descendant nodes.

Example usage:

final nodeCount = doc.fold<int>(0, (acc, _) => acc + 1);

Implementation

T fold<T>(T initial, T Function(T acc, LatexNode node) combine) {
  var acc = combine(initial, this);
  for (final child in children()) {
    acc = child.fold(acc, combine);
  }
  return acc;
}