From 846014b37f1d5445d8a7666c9be5253c17dbc18c Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Mon, 15 Dec 2014 18:11:29 +0300 Subject: [PATCH] Add missing sum() on Streams. --- libraries/stdlib/src/generated/_Numeric.kt | 48 +++++++++++++++++++ .../stdlib/test/collections/CollectionTest.kt | 1 + .../src/generators/GenerateCollections.kt | 1 + 3 files changed, 50 insertions(+) diff --git a/libraries/stdlib/src/generated/_Numeric.kt b/libraries/stdlib/src/generated/_Numeric.kt index ceaa2b4826a..100cf4decf0 100644 --- a/libraries/stdlib/src/generated/_Numeric.kt +++ b/libraries/stdlib/src/generated/_Numeric.kt @@ -19,6 +19,18 @@ public fun Iterable.sum(): Int { return sum } +/** + * Returns the sum of all elements in the collection + */ +public fun Stream.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + /** * Returns the sum of all elements in the collection */ @@ -31,6 +43,18 @@ public fun Iterable.sum(): Long { return sum } +/** + * Returns the sum of all elements in the collection + */ +public fun Stream.sum(): Long { + val iterator = iterator() + var sum: Long = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + /** * Returns the sum of all elements in the collection */ @@ -43,6 +67,18 @@ public fun Iterable.sum(): Double { return sum } +/** + * Returns the sum of all elements in the collection + */ +public fun Stream.sum(): Double { + val iterator = iterator() + var sum: Double = 0.0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + /** * Returns the sum of all elements in the collection */ @@ -55,6 +91,18 @@ public fun Iterable.sum(): Float { return sum } +/** + * Returns the sum of all elements in the collection + */ +public fun Stream.sum(): Float { + val iterator = iterator() + var sum: Float = 0.0f + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + /** * Returns the sum of all elements in the collection */ diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 8100b53f72f..9d33a1ec067 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -435,6 +435,7 @@ class CollectionTest { expect(3.0) { arrayListOf(1.0, 2.0).sum() } expect(3000000000000) { arrayListOf(1000000000000, 2000000000000).sum() } expect(3.0.toFloat()) { arrayListOf(1.0.toFloat(), 2.0.toFloat()).sum() } + expect(3.0.toFloat()) { streamOf(1.0.toFloat(), 2.0.toFloat()).sum() } } test fun takeReturnsFirstNElements() { diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt index bc4deb906ad..2d0891dc9df 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt @@ -25,6 +25,7 @@ fun generateCollectionsAPI(outDir: File) { // TODO: decide if sum for byte and short is needed and how to make it work for (numeric in listOf(Int, Long, /*Byte, Short, */ Double, Float)) { build(builder, Iterables, numeric) + build(builder, Streams, numeric) } for (numeric in listOf(Int, Long, Byte, Short, Double, Float)) {