replaceAll method
- @Possible({ConcurrentModificationError})
- void function(
- Consume<
E> add, - E element
- Consume<
Replaces elements using function
.
function
accepts a Consume for specifying an element's replacements. An element can be replaced by zero or more
elements. This function is an in-place 1:N map function.
Contract
A ConcurrentModificationError is thrown if function
directly modifies this list.
final foo = [1];
foo.replaceAll((_, __) => foo.remove(0)); // throws ConcurrentModificationError
Example
void multiplyOdd(Consume<int> add, int element) {
if (element.isOdd)
replace(element * 10);
}
[1, 2, 3, 4].replaceAll(multiplyOdd); // [10, 30]
Implementation
@Possible({ConcurrentModificationError})
void replaceAll(void Function(Consume<E> add, E element) function) {
final retained = <E>[];
final length = this.length;
for (final element in this) {
function(retained.add, element);
if (length != this.length) {
throw ConcurrentModificationError(this);
}
}
clear();
addAll(retained);
}