From f44cc311bd672013c4b230960004fcab7e20dd60 Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Sat, 22 Aug 2020 12:30:44 +0200 Subject: [PATCH 1/4] Added aggregate operations --- .../kotlin/collections/AggregateOperations.kt | 131 ++++++++++++++++++ .../AggregateOperationsUnitTest.kt | 104 ++++++++++++++ 2 files changed, 235 insertions(+) create mode 100644 core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt create mode 100644 core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/kotlin/collections/AggregateOperationsUnitTest.kt diff --git a/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt b/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt new file mode 100644 index 0000000000..854190382c --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt @@ -0,0 +1,131 @@ +package com.baeldung.kotlin.collections + +class AggregateOperations { + private val numbers = listOf(1, 15, 3, 8) + + fun countList(): Int { + return numbers.count() + } + + fun sumList(): Int { + return numbers.sum() + } + + fun averageList(): Double { + return numbers.average() + } + + fun maximumInList(): Int? { + return numbers.max() + } + + fun minimumInList(): Int? { + return numbers.min() + } + + fun maximumByList(): Int? { + return numbers.maxBy { it % 5 } + } + + fun minimumByList(): Int? { + return numbers.minBy { it % 5 } + } + + fun maximumWithList(): String? { + val strings = listOf("Berlin", "Kolkata", "Prague", "Barcelona") + return strings.maxWith(compareBy { it.length % 4 }) + } + + fun minimumWithList(): String? { + val strings = listOf("Berlin", "Kolkata", "Prague", "Barcelona") + return strings.minWith(compareBy { it.length % 4 }) + } + + fun sumByList(): Int { + return numbers.sumBy { it * 5 } + } + + fun sumByDoubleList(): Double { + return numbers.sumByDouble { it.toDouble() / 8 } + } + + fun foldList(): Int { + println("fold operation") + val result = numbers.fold(100) { total, it -> + println("total = $total, it = $it") + total - it + } + println(result) // ((((100 - 1)-15)-3)-8) = 73 + return result + } + + fun foldRightList(): Int { + println("foldRight operation") + val result = numbers.foldRight(100) { it, total -> + println("total = $total, it = $it") + total - it + } + println(result) // ((((100-8)-3)-15)-1) = 73 + return result + } + + fun foldIndexedList(): Int { + println("foldIndexed operation") + val result = numbers.foldIndexed(100) { index, total, it -> + println("total = $total, it = $it, index = $index") + if (index.minus(2) >= 0) total - it else total + } + println(result) // ((100 - 3)-8) = 89 + return result + } + + fun foldRightIndexedList(): Int { + println("foldRightIndexed operation") + val result = numbers.foldRightIndexed(100) { index, it, total -> + println("total = $total, it = $it, index = $index") + if (index.minus(2) >= 0) total - it else total + } + println(result) // ((100 - 8)-3) = 89 + return result + } + + fun reduceList(): Int { + println("reduce operation") + val result = numbers.reduce { total, it -> + println("total = $total, it = $it") + total - it + } + println(result) // (((1 - 15)-3)-8) = -25 + return result + } + + fun reduceRightList(): Int { + println("reduceRight operation") + val result = numbers.reduceRight() { it, total -> + println("total = $total, it = $it") + total - it + } + println(result) // ((8-3)-15)-1) = -11 + return result + } + + fun reduceIndexedList(): Int { + println("reduceIndexed operation") + val result = numbers.reduceIndexed { index, total, it -> + println("total = $total, it = $it, index = $index") + if (index.minus(2) >= 0) total - it else total + } + println(result) // ((1-3)-8) = -10 + return result + } + + fun reduceRightIndexedList(): Int { + println("reduceRightIndexed operation") + val result = numbers.reduceRightIndexed { index, it, total -> + println("total = $total, it = $it, index = $index") + if (index.minus(2) >= 0) total - it else total + } + println(result) // ((8-3) = 5 + return result + } +} diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/kotlin/collections/AggregateOperationsUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/kotlin/collections/AggregateOperationsUnitTest.kt new file mode 100644 index 0000000000..1fb26760fc --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/kotlin/collections/AggregateOperationsUnitTest.kt @@ -0,0 +1,104 @@ +package com.baeldung.kotlin.collections + +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class AggregateOperationsUnitTest { + + private val classUnderTest: AggregateOperations = AggregateOperations() + + @Test + fun whenCountOfList_thenReturnsValue() { + assertEquals(4, classUnderTest.countList()) + } + + @Test + fun whenSumOfList_thenReturnsTotalValue() { + assertEquals(27, classUnderTest.sumList()) + } + + @Test + fun whenAverageOfList_thenReturnsValue() { + assertEquals(6.75, classUnderTest.averageList()) + } + + @Test + fun whenMaximumOfList_thenReturnsMaximumValue() { + assertEquals(15, classUnderTest.maximumInList()) + } + + @Test + fun whenMinimumOfList_thenReturnsMinimumValue() { + assertEquals(1, classUnderTest.minimumInList()) + } + + @Test + fun whenMaxByList_thenReturnsLargestValue() { + assertEquals(3, classUnderTest.maximumByList()) + } + + @Test + fun whenMinByList_thenReturnsSmallestValue() { + assertEquals(15, classUnderTest.minimumByList()) + } + + @Test + fun whenMaxWithList_thenReturnsLargestValue(){ + assertEquals("Kolkata", classUnderTest.maximumWithList()) + } + + @Test + fun whenMinWithList_thenReturnsSmallestValue(){ + assertEquals("Barcelona", classUnderTest.minimumWithList()) + } + + @Test + fun whenSumByList_thenReturnsIntegerValue(){ + assertEquals(135, classUnderTest.sumByList()) + } + + @Test + fun whenSumByDoubleList_thenReturnsDoubleValue(){ + assertEquals(3.375, classUnderTest.sumByDoubleList()) + } + + @Test + fun whenFoldList_thenReturnsValue(){ + assertEquals(73, classUnderTest.foldList()) + } + + @Test + fun whenFoldRightList_thenReturnsValue(){ + assertEquals(73, classUnderTest.foldRightList()) + } + + @Test + fun whenFoldIndexedList_thenReturnsValue(){ + assertEquals(89, classUnderTest.foldIndexedList()) + } + + @Test + fun whenFoldRightIndexedList_thenReturnsValue(){ + assertEquals(89, classUnderTest.foldRightIndexedList()) + } + + @Test + fun whenReduceList_thenReturnsValue(){ + assertEquals(-25, classUnderTest.reduceList()) + } + + @Test + fun whenReduceRightList_thenReturnsValue(){ + assertEquals(-11, classUnderTest.reduceRightList()) + } + + @Test + fun whenReduceIndexedList_thenReturnsValue(){ + assertEquals(-10, classUnderTest.reduceIndexedList()) + } + + @Test + fun whenReduceRightIndexedList_thenReturnsValue(){ + assertEquals(5, classUnderTest.reduceRightIndexedList()) + } +} \ No newline at end of file From 82203b222e89543accda497ec9450db0adbb6e73 Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Mon, 24 Aug 2020 09:59:39 +0200 Subject: [PATCH 2/4] Removed print statements --- .../kotlin/collections/AggregateOperations.kt | 56 ++++++------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt b/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt index 854190382c..3a348aafaa 100644 --- a/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt +++ b/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt @@ -50,82 +50,58 @@ class AggregateOperations { } fun foldList(): Int { - println("fold operation") - val result = numbers.fold(100) { total, it -> + return numbers.fold(100) { total, it -> println("total = $total, it = $it") total - it - } - println(result) // ((((100 - 1)-15)-3)-8) = 73 - return result + } // ((((100 - 1)-15)-3)-8) = 73 } fun foldRightList(): Int { - println("foldRight operation") - val result = numbers.foldRight(100) { it, total -> + return numbers.foldRight(100) { it, total -> println("total = $total, it = $it") total - it - } - println(result) // ((((100-8)-3)-15)-1) = 73 - return result + } // ((((100-8)-3)-15)-1) = 73 } fun foldIndexedList(): Int { - println("foldIndexed operation") - val result = numbers.foldIndexed(100) { index, total, it -> + return numbers.foldIndexed(100) { index, total, it -> println("total = $total, it = $it, index = $index") if (index.minus(2) >= 0) total - it else total - } - println(result) // ((100 - 3)-8) = 89 - return result + } // ((100 - 3)-8) = 89 } fun foldRightIndexedList(): Int { - println("foldRightIndexed operation") - val result = numbers.foldRightIndexed(100) { index, it, total -> + return numbers.foldRightIndexed(100) { index, it, total -> println("total = $total, it = $it, index = $index") if (index.minus(2) >= 0) total - it else total - } - println(result) // ((100 - 8)-3) = 89 - return result + } // ((100 - 8)-3) = 89 } fun reduceList(): Int { - println("reduce operation") - val result = numbers.reduce { total, it -> + return numbers.reduce { total, it -> println("total = $total, it = $it") total - it - } - println(result) // (((1 - 15)-3)-8) = -25 - return result + } // (((1 - 15)-3)-8) = -25 } fun reduceRightList(): Int { - println("reduceRight operation") - val result = numbers.reduceRight() { it, total -> + return numbers.reduceRight() { it, total -> println("total = $total, it = $it") total - it - } - println(result) // ((8-3)-15)-1) = -11 - return result + } // ((8-3)-15)-1) = -11 } fun reduceIndexedList(): Int { - println("reduceIndexed operation") - val result = numbers.reduceIndexed { index, total, it -> + return numbers.reduceIndexed { index, total, it -> println("total = $total, it = $it, index = $index") if (index.minus(2) >= 0) total - it else total - } - println(result) // ((1-3)-8) = -10 - return result + } // ((1-3)-8) = -10 } fun reduceRightIndexedList(): Int { - println("reduceRightIndexed operation") - val result = numbers.reduceRightIndexed { index, it, total -> + return numbers.reduceRightIndexed { index, it, total -> println("total = $total, it = $it, index = $index") if (index.minus(2) >= 0) total - it else total - } - println(result) // ((8-3) = 5 - return result + } // ((8-3) = 5 } } From 531751568cacd590d923b17d1b41940872c46da9 Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Sun, 30 Aug 2020 13:23:51 +0200 Subject: [PATCH 3/4] Moved to new module --- .../core-kotlin-collections-2/README.md | 7 +++ .../core-kotlin-collections-2/pom.xml | 47 +++++++++++++++++++ .../AggregateOperations.kt | 2 +- .../AggregateOperationsUnitTest.kt | 2 +- core-kotlin-modules/pom.xml | 1 + 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 core-kotlin-modules/core-kotlin-collections-2/README.md create mode 100644 core-kotlin-modules/core-kotlin-collections-2/pom.xml rename core-kotlin-modules/{core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections => core-kotlin-collections-2/src/main/kotlin/com.baeldung.aggregate}/AggregateOperations.kt (98%) rename core-kotlin-modules/{core-kotlin-collections/src/test/kotlin/com/baeldung/kotlin/collections => core-kotlin-collections-2/src/test/kotlin/com.baeldung.aggregate}/AggregateOperationsUnitTest.kt (98%) diff --git a/core-kotlin-modules/core-kotlin-collections-2/README.md b/core-kotlin-modules/core-kotlin-collections-2/README.md new file mode 100644 index 0000000000..2dc180b5b3 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections-2/README.md @@ -0,0 +1,7 @@ +## Core Kotlin Collections + +This module contains articles about core Kotlin collections. + +### Relevant articles: + + diff --git a/core-kotlin-modules/core-kotlin-collections-2/pom.xml b/core-kotlin-modules/core-kotlin-collections-2/pom.xml new file mode 100644 index 0000000000..be462eed45 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections-2/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + core-kotlin-collections-2 + core-kotlin-collections-2 + jar + + + com.baeldung.core-kotlin-modules + core-kotlin-modules + 1.0.0-SNAPSHOT + + + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + org.apache.commons + commons-math3 + ${commons-math3.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + + + + + 1.3.30 + 3.6.1 + 3.10.0 + + + \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt b/core-kotlin-modules/core-kotlin-collections-2/src/main/kotlin/com.baeldung.aggregate/AggregateOperations.kt similarity index 98% rename from core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt rename to core-kotlin-modules/core-kotlin-collections-2/src/main/kotlin/com.baeldung.aggregate/AggregateOperations.kt index 3a348aafaa..a09e101b59 100644 --- a/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/kotlin/collections/AggregateOperations.kt +++ b/core-kotlin-modules/core-kotlin-collections-2/src/main/kotlin/com.baeldung.aggregate/AggregateOperations.kt @@ -1,4 +1,4 @@ -package com.baeldung.kotlin.collections +package com.baeldung.aggregate class AggregateOperations { private val numbers = listOf(1, 15, 3, 8) diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/kotlin/collections/AggregateOperationsUnitTest.kt b/core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/com.baeldung.aggregate/AggregateOperationsUnitTest.kt similarity index 98% rename from core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/kotlin/collections/AggregateOperationsUnitTest.kt rename to core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/com.baeldung.aggregate/AggregateOperationsUnitTest.kt index 1fb26760fc..a619759b0a 100644 --- a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/kotlin/collections/AggregateOperationsUnitTest.kt +++ b/core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/com.baeldung.aggregate/AggregateOperationsUnitTest.kt @@ -1,4 +1,4 @@ -package com.baeldung.kotlin.collections +package com.baeldung.aggregate import org.junit.jupiter.api.Test import kotlin.test.assertEquals diff --git a/core-kotlin-modules/pom.xml b/core-kotlin-modules/pom.xml index 8b626e1c1b..67520a7dee 100644 --- a/core-kotlin-modules/pom.xml +++ b/core-kotlin-modules/pom.xml @@ -21,6 +21,7 @@ core-kotlin-advanced core-kotlin-annotations core-kotlin-collections + core-kotlin-collections-2 core-kotlin-concurrency core-kotlin-date-time core-kotlin-design-patterns From c6d95556171005b68cc4a6194de97de2cb5406ff Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Mon, 31 Aug 2020 20:38:06 +0200 Subject: [PATCH 4/4] Refactored package --- .../baeldung/aggregate}/AggregateOperations.kt | 0 .../baeldung/aggregate}/AggregateOperationsUnitTest.kt | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename core-kotlin-modules/core-kotlin-collections-2/src/main/kotlin/{com.baeldung.aggregate => com/baeldung/aggregate}/AggregateOperations.kt (100%) rename core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/{com.baeldung.aggregate => com/baeldung/aggregate}/AggregateOperationsUnitTest.kt (100%) diff --git a/core-kotlin-modules/core-kotlin-collections-2/src/main/kotlin/com.baeldung.aggregate/AggregateOperations.kt b/core-kotlin-modules/core-kotlin-collections-2/src/main/kotlin/com/baeldung/aggregate/AggregateOperations.kt similarity index 100% rename from core-kotlin-modules/core-kotlin-collections-2/src/main/kotlin/com.baeldung.aggregate/AggregateOperations.kt rename to core-kotlin-modules/core-kotlin-collections-2/src/main/kotlin/com/baeldung/aggregate/AggregateOperations.kt diff --git a/core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/com.baeldung.aggregate/AggregateOperationsUnitTest.kt b/core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/com/baeldung/aggregate/AggregateOperationsUnitTest.kt similarity index 100% rename from core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/com.baeldung.aggregate/AggregateOperationsUnitTest.kt rename to core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/com/baeldung/aggregate/AggregateOperationsUnitTest.kt