Repository: passsy/kt.dart (dart)

Issue:
Add sumOf()
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sum-of.html

Pull request description:
Closes https://github.com/passsy/kt.dart/issues/154

Patch:
diff --git a/lib/src/collection/kt_collection.dart b/lib/src/collection/kt_collection.dart
index 734f0b7..71c6189 100644
--- a/lib/src/collection/kt_collection.dart
+++ b/lib/src/collection/kt_collection.dart
@@ -47,6 +47,15 @@ extension KtCollectionExtensions<T> on KtCollection<T> {
     return elementAt(index);
   }
 
+  /// Returns the sum of all elements in this collection.
+  R sumOf<R extends num>(R Function(T) selector) {
+    var sum = R == double ? 0.0 : 0;
+    for (final element in iter) {
+      sum += selector(element);
+    }
+    return sum as R;
+  }
+
   /// Returns a [KtMutableList] filled with all elements of this collection.
   KtMutableList<T> toMutableList() => KtMutableList<T>.from(iter);
 }


Test patch:
diff --git a/test/collection/collection_test.dart b/test/collection/collection_test.dart
index 91e8313..b41c4a2 100644
--- a/test/collection/collection_test.dart
+++ b/test/collection/collection_test.dart
@@ -173,6 +173,38 @@ void testCollection(KtCollection<T> Function<T>() emptyCollection,
       expect(emptyCollection.randomOrNull(), equals(null));
     });
   });
+
+  group("sumOf", () {
+    test("calculates the sum of an integer list", () {
+      final collection = collectionOf([1, 2, 3]);
+      final sum = collection.sumOf((it) => it);
+      expect(sum, 6);
+      expect(sum.runtimeType, int);
+    });
+
+    test("calculates the sum of a double list", () {
+      final collection = collectionOf([1.1, 2.2, 3.3]);
+      final sum = collection.sumOf((it) => it);
+      expect(sum, 6.6);
+      expect(sum.runtimeType, double);
+    });
+
+    test("calculates the sum of a list created with ints and doubles", () {
+      final collection = collectionOf([0, 1.1, 2, 3.3]);
+      final sum = collection.sumOf((it) => it);
+      expect(sum, 6.4);
+      expect(sum.runtimeType, double);
+    });
+
+    test("calculates the sum of the length of each element of a string list",
+        () {
+      final collection = collectionOf(['a', 'xyz', 'Hello World']);
+      final sum = collection.sumOf((it) => it.length);
+      expect(sum, 15);
+      expect(sum.runtimeType, int);
+    });
+  });
+
   group("toString", () {
     if (ordered) {
       test("default string representation", () {