Repository: passsy/kt.dart (dart)

Issue:
Add flatMapIndexed
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/flat-map-indexed.html

Pull request description:
fixes #147

Patch:
diff --git a/lib/src/collection/kt_iterable.dart b/lib/src/collection/kt_iterable.dart
index 22c5a79..dee0c36 100644
--- a/lib/src/collection/kt_iterable.dart
+++ b/lib/src/collection/kt_iterable.dart
@@ -629,6 +629,29 @@ extension KtIterableExtensions<T> on KtIterable<T> {
     return list;
   }
 
+  /// Returns a single list of all elements yielded from results of [transform]
+  /// function being invoked on each element and its index in the original
+  /// collection.
+  KtList<R> flatMapIndexed<R>(KtIterable<R> Function(int index, T) transform) {
+    final list = flatMapIndexedTo(mutableListOf<R>(), transform);
+    // making a temp variable here, it helps dart to get types right ¯\_(ツ)_/¯
+    // TODO ping dort-lang/sdk team to check that bug
+    return list;
+  }
+
+  /// Appends all elements yielded from results of [transform] function being
+  /// invoked on each element and its index in the original collection, to the
+  /// given [destination].
+  C flatMapIndexedTo<R, C extends KtMutableCollection<R>>(
+      C destination, KtIterable<R> Function(int index, T) transform) {
+    var index = 0;
+    for (final element in iter) {
+      final list = transform(index++, element);
+      destination.addAll(list);
+    }
+    return destination;
+  }
+
   /// Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination].
   C flatMapTo<R, C extends KtMutableCollection<R>>(
       C destination, KtIterable<R> Function(T) transform) {


Test patch:
diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart
index 02e7a0e..2be2b2c 100644
--- a/test/collection/iterable_extensions_test.dart
+++ b/test/collection/iterable_extensions_test.dart
@@ -834,6 +834,32 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
     });
   });
 
+  group("flatMapTo", () {
+    test("flatMapTo int to string", () {
+      final result = mutableListOf<int>();
+      final iterable = iterableOf([1, 2, 3]);
+      iterable.flatMapTo(result, (it) => iterableOf([it, it + 1, it + 2]));
+      expect(result, listOf(1, 2, 3, 2, 3, 4, 3, 4, 5));
+    });
+  });
+
+  group("flatMapIndexed", () {
+    test("flatMapIndexed int to string", () {
+      final iterable = iterableOf([1, 2, 3]);
+      expect(iterable.flatMapIndexed((ix, it) => iterableOf([ix, it])).toList(),
+          listOf(0, 1, 1, 2, 2, 3));
+    });
+  });
+
+  group("flatMapIndexedTo", () {
+    test("flatMapIndexedTo int to string", () {
+      final result = mutableListOf<int>();
+      final iterable = iterableOf([1, 2, 3]);
+      iterable.flatMapIndexedTo(result, (ix, it) => iterableOf([ix, it]));
+      expect(result, listOf(0, 1, 1, 2, 2, 3));
+    });
+  });
+
   group("flatten", () {
     test("empty", () {
       final KtIterable<KtIterable<int>> nested =