Skip to content

Instantly share code, notes, and snippets.

@eric-taix
Last active October 31, 2023 09:43
Show Gist options
  • Save eric-taix/12d2c7dd69385cd109b9662de5328ea8 to your computer and use it in GitHub Desktop.
Save eric-taix/12d2c7dd69385cd109b9662de5328ea8 to your computer and use it in GitHub Desktop.
void main() {
final l = List.generate(5, (index) => index * 2);
print(l);
print(l.insertBetween(100));
print(l.insertBetweenAndAround(100));
print(l.fillUpTo(count: 10, value: 100));
print(l.fillUpTo(count: 3, value: 100));
}
extension IterableExtension<T> on Iterable<T> {
Iterable<T> insertBetween(T value) {
return expand((v) => [v, value]).take(length * 2 - 1);
}
Iterable<T> insertBetweenAndAround(T value) {
return [
value,
...expand((v) => [v, value])
];
}
Iterable<T> fillUpTo({required int count, required T value}) {
return [
...this,
if (count > length) ...List.filled(count - length, value),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment