From f44cc311bd672013c4b230960004fcab7e20dd60 Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Sat, 22 Aug 2020 12:30:44 +0200 Subject: [PATCH] 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