separate<To> method
Apply convert to each item and insert the separator between
each <groupSize> items (not including the beginning and the end).
Returns a new Iterable (not lazy) with the converted items and separators.
Example
final nums = [1, 2, 3];
final result = nums.separate(convert: (n) => n * 2, separator: -1);
print(result);
// Output:
// (2, -1, 4, -1, 6)
Implementation
Iterable<To> separate<To>({
required To Function(E) convert,
required To separator,
int groupSize = 1,
}) sync* {
assert(groupSize > 0);
int counter = 0;
for (final E entry in this) {
if (counter == groupSize) {
counter = 0;
yield separator;
}
counter += 1;
yield convert(entry);
}
}