countAndReplace method

Iterable<T> countAndReplace(
  1. T target,
  2. T replacer(
    1. int count
    )
)

Implementation

Iterable<T> countAndReplace(T target, T Function(int count) replacer) sync* {
  Iterator<T> iterator = this.iterator;
  int count = 0;
  while (iterator.moveNext()) {
    T element = iterator.current;
    if (element == target) {
      count++;
    } else {
      if (count > 0) {
        yield replacer(count);
        count = 0;
      }
      yield element;
    }
  }
  if (count > 0) {
    yield replacer(count);
  }
}