diff --git a/lib/src/collection/kt_iterable.dart b/lib/src/collection/kt_iterable.dart index c440c02..613dd45 100644 --- a/lib/src/collection/kt_iterable.dart +++ b/lib/src/collection/kt_iterable.dart @@ -1118,21 +1118,6 @@ extension KtIterableExtensions on KtIterable { return true; } - /// Performs the given [action] on each element. Use with cascade syntax to return self. - /// - /// (listOf("a", "b", "c") - /// ..onEach(print)) - /// .map((it) => it.toUpperCase()) - /// .getOrNull(0); // prints: a - /// - // ignore: comment_references - /// Without the cascade syntax (..) [KtListExtensions.getOrNull] wouldn't be available. - void onEach(void Function(T) action) { - for (final element in iter) { - action(element); - } - } - /// Splits the original collection into pair of lists, /// where *first* list contains elements for which [predicate] yielded `true`, /// while *second* list contains elements for which [predicate] yielded `false`. @@ -1515,6 +1500,35 @@ extension RequireNoNullsKtIterableExtension on KtIterable { } } +// TODO: Should be > on C but then T resolves to dynamic +// https://github.com/dart-lang/sdk/issues/46117 +extension ChainableKtIterableExtensions on KtIterable { + /// Performs the given [action] on each element. Use with cascade syntax to return self. + /// + /// final result = listOf("a", "b", "c") + /// .onEach(print) + /// .map((it) => it.toUpperCase()) + /// .getOrNull(0); + /// // result: A + /// + /// Without the cascade syntax (..) [KtListExtensions.getOrNull] wouldn't be available. + KtIterable onEach(void Function(T item) action) { + for (final element in iter) { + action(element); + } + return this; + } + + /// Performs the given action on each element, providing sequential index with the element, and returns the collection itself afterwards. + KtIterable onEachIndexed(void Function(int index, T item) action) { + var index = 0; + for (final item in iter) { + action(index++, item); + } + return this; + } +} + class _MovingSubList { _MovingSubList(this.list); diff --git a/lib/src/collection/kt_list.dart b/lib/src/collection/kt_list.dart index 42255a2..b2c4a57 100644 --- a/lib/src/collection/kt_list.dart +++ b/lib/src/collection/kt_list.dart @@ -383,6 +383,34 @@ extension NullableKtListExtensions on KtList? { KtList orEmpty() => this ?? KtList.empty(); } +// TODO remove in favor of ChainableKtIterableExtensions once https://github.com/dart-lang/sdk/issues/46117 is resolved +extension ChainableKtListExtensions on KtList { + /// Performs the given [action] on each element. Use with cascade syntax to return self. + /// + /// final result = listOf("a", "b", "c") + /// .onEach(print) + /// .map((it) => it.toUpperCase()) + /// .getOrNull(0); + /// // result: A + /// + /// Without the cascade syntax (..) [KtListExtensions.getOrNull] wouldn't be available. + KtList onEach(void Function(T item) action) { + for (final element in iter) { + action(element); + } + return this; + } + + /// Performs the given action on each element, providing sequential index with the element, and returns the collection itself afterwards. + KtList onEachIndexed(void Function(int index, T item) action) { + var index = 0; + for (final item in iter) { + action(index++, item); + } + return this; + } +} + extension RequireNoNullsKtListExtension on KtList { /// Returns an original collection containing all the non-`null` elements, throwing an [ArgumentError] if there are any `null` elements. KtList requireNoNulls() { diff --git a/lib/src/collection/kt_set.dart b/lib/src/collection/kt_set.dart index 471b530..b800521 100644 --- a/lib/src/collection/kt_set.dart +++ b/lib/src/collection/kt_set.dart @@ -152,3 +152,31 @@ extension NullableKtSetExtensions on KtSet? { /// Returns this [KtSet] if it's not `null` and the empty set otherwise. KtSet orEmpty() => this ?? KtSet.empty(); } + +// TODO remove in favor of ChainableKtIterableExtensions once https://github.com/dart-lang/sdk/issues/46117 is resolved +extension ChainableKtSetExtensions on KtSet { + /// Performs the given [action] on each element. Use with cascade syntax to return self. + /// + /// final result = setOf("a", "b", "c") + /// .onEach(print) + /// .map((it) => it.toUpperCase()) + /// .getOrNull(0); + /// // result: A + /// + /// Without the cascade syntax (..) [KtListExtensions.getOrNull] wouldn't be available. + KtSet onEach(void Function(T item) action) { + for (final element in iter) { + action(element); + } + return this; + } + + /// Performs the given action on each element, providing sequential index with the element, and returns the collection itself afterwards. + KtSet onEachIndexed(void Function(int index, T item) action) { + var index = 0; + for (final item in iter) { + action(index++, item); + } + return this; + } +} diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart index 1e64f81..f781eaf 100644 --- a/test/collection/iterable_extensions_test.dart +++ b/test/collection/iterable_extensions_test.dart @@ -1514,6 +1514,45 @@ void testIterable(KtIterable Function() emptyIterable, iterable.onEach((it) => it.add(0)); expect(iterable.map((it) => it.last).toList(), listOf(0, 0, 0)); }); + + test("chainable", () { + final list = KtMutableList.empty(); + final result = listOf("a", "b", "c") + .onEach((it) => list.add(it)) + .map((it) => it.toUpperCase()) + .getOrNull(0); // prints: A + expect(result, "A"); + expect(list, listOf("a", "b", "c")); + }); + }); + + group("onEachIndexed", () { + test("pairs", () { + final indexes = KtMutableList.empty(); + final items = KtMutableList.empty(); + final iterable = iterableOf([1, 2, 3]); + iterable.onEachIndexed((index, item) { + indexes.add(index); + items.add(item); + }); + expect(indexes, listOf(0, 1, 2)); + if (ordered) { + expect(items, iterable.toList()); + } + }); + + test("chainable", () { + final list = KtMutableList.empty(); + final result = listOf("a", "b", "c") + .onEachIndexed((index, it) { + list.add(index); + list.add(it); + }) + .map((it) => it.toUpperCase()) + .getOrNull(0); // prints: A + expect(result, "A"); + expect(list, listOf(0, "a", 1, "b", 2, "c")); + }); }); group("partition", () {