Last active
October 31, 2023 09:43
-
-
Save eric-taix/12d2c7dd69385cd109b9662de5328ea8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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