Created
June 29, 2020 22:05
-
-
Save ltOgt/f8da60e1f64b7fa509c6706e41629886 to your computer and use it in GitHub Desktop.
This file contains 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
extension ForEachIndexedExtension<E> on List<E> { | |
/// Returns a new eagerly computed [List] with elements of type [T] that are created by | |
/// calling `f` on each element of this `List` with elements of type [E] in order of increasing index. | |
/// | |
/// `f` exposes the index and the element at that index. | |
/// | |
/// ___________ | |
/// For example: | |
/// | |
/// ``` | |
/// ["a", "b", "c"].forEachIndexed((int index, String element) => {index: element}); | |
/// ``` | |
/// | |
/// Returns `[{0: a}, {1: b}, {2: c}]` | |
/// | |
List<T> forEachIndexed<T>(T f(int index, E e)) { | |
List<T> r = []; | |
for (int i = 0; i < this.length; i++) { | |
r.add( | |
f(i, this.elementAt(i)), | |
); | |
} | |
return r; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See extension methods for more information.