replaceFirstWhere method

Iterable<T> replaceFirstWhere(
  1. bool comparator(
    1. T currentValue
    ),
  2. T newValue
)

Replaces the first element that matches the comparator with newValue.

Example:

[1, 2, 3].replaceFirstWhere((n) => n < 3, 0); // [0, 2, 3]

Implementation

Iterable<T> replaceFirstWhere(
  bool Function(T currentValue) comparator,
  T newValue,
) sync* {
  final it = iterator;
  while (it.moveNext()) {
    if (comparator(it.current)) {
      yield newValue;
      while (it.moveNext()) {
        yield it.current;
      }
    } else {
      yield it.current;
    }
  }
}