Skip to content

Instantly share code, notes, and snippets.

@ltOgt
Created June 29, 2020 22:05
Show Gist options
  • Save ltOgt/f8da60e1f64b7fa509c6706e41629886 to your computer and use it in GitHub Desktop.
Save ltOgt/f8da60e1f64b7fa509c6706e41629886 to your computer and use it in GitHub Desktop.
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;
}
}
@ltOgt
Copy link
Author

ltOgt commented Jun 29, 2020

See extension methods for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment