diff --git a/tp8/src/main/kotlin/fmt/kotlin/fundamentals/Cellar.kt b/tp8/src/main/kotlin/fmt/kotlin/fundamentals/Cellar.kt new file mode 100644 index 0000000..8e08e7d --- /dev/null +++ b/tp8/src/main/kotlin/fmt/kotlin/fundamentals/Cellar.kt @@ -0,0 +1,17 @@ +package fmt.kotlin.fundamentals + +data class Vineyard( + val cellars: List +) { + + fun countBottles() = emptyMap() +} + +data class Cellar( + val bottles: List +) + +data class Bottle( + val name: String, + val year: Int +) diff --git a/tp8/src/main/kotlin/fmt/kotlin/fundamentals/MapExt.kt b/tp8/src/main/kotlin/fmt/kotlin/fundamentals/MapExt.kt new file mode 100644 index 0000000..fc738fa --- /dev/null +++ b/tp8/src/main/kotlin/fmt/kotlin/fundamentals/MapExt.kt @@ -0,0 +1,21 @@ +package fmt.kotlin.fundamentals + +/** + * Merge with [map] using [operation] to compute the new value. + * + * If one map have no entry for a key, operation is not called and the existing value is used. + * + * @param K the type of keys in the maps + * @param V the type of values in the maps + * @param map the map to merge with + * @param operation an operation to call on entry with a value in both map to compute the new value + * @return a new map with keys of both maps and values computed with [operation] + */ +fun Map.merge(map: Map, operation: (V, V) -> V): Map = + (keys + map.keys).associateWith { key -> + if (this[key] != null && map[key] != null) { + operation(this[key]!!, map[key]!!) + } else { + this[key] ?: map[key]!! + } + } diff --git a/tp8/src/test/kotlin/fmt/kotlin/fundamentals/VineyardTest.kt b/tp8/src/test/kotlin/fmt/kotlin/fundamentals/VineyardTest.kt new file mode 100644 index 0000000..9fab9de --- /dev/null +++ b/tp8/src/test/kotlin/fmt/kotlin/fundamentals/VineyardTest.kt @@ -0,0 +1,62 @@ +package fmt.kotlin.fundamentals + +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test +import strikt.api.expectThat +import strikt.assertions.isEqualTo + +class VineyardTest { + + val vineyard = Vineyard( + listOf( + Cellar( + listOf( + Bottle("Coucheroy", 2005), + Bottle("Coucheroy", 2005), + Bottle("Coucheroy", 2015), + Bottle("Coucheroy", 2015), + Bottle("Pedesclaux", 2009), + Bottle("Pedesclaux", 2009), + Bottle("Pedesclaux", 2009), + ) + ), + Cellar( + listOf( + Bottle("Coucheroy", 2015), + Bottle("Coucheroy", 2015), + Bottle("Coucheroy", 2015), + Bottle("Pedesclaux", 2009), + Bottle("Pedesclaux", 2009), + Bottle("Talbot", 2009), + Bottle("Talbot", 2015), + Bottle("Pedesclaux", 2009), + ) + ), + Cellar( + listOf( + Bottle("Talbot", 2005), + Bottle("Talbot", 2005), + Bottle("Talbot", 2005), + Bottle("Talbot", 2005), + Bottle("Pedesclaux", 2009), + Bottle("Pedesclaux", 2009), + Bottle("Pedesclaux", 2009), + ) + ) + ) + ) + + + @Test + fun `should count bottles in all cellars`() { + val count = vineyard.countBottles() + + expectThat(count).isEqualTo( + mapOf( + "Coucheroy" to 7, + "Pedesclaux" to 9, + "Talbot" to 6 + ) + ) + } +} \ No newline at end of file