Skip to content

Commit 33dc6a5

Browse files
ctruchiCLOVIS-AI
authored andcommitted
tp8 step 3 solution
1 parent e7502ca commit 33dc6a5

File tree

1 file changed

+8
-1
lines changed
  • tp8/src/main/kotlin/fmt/kotlin/fundamentals

1 file changed

+8
-1
lines changed

tp8/src/main/kotlin/fmt/kotlin/fundamentals/MapExt.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ package fmt.kotlin.fundamentals
99
* @param operation an operation to call on entry with a value in both map to compute the new value
1010
* @return a new map with keys of both maps and values computed with [operation]
1111
*/
12-
fun Map<String, Int>.merge(map: Map<String, Int>, operation: (Int, Int) -> Int): Map<String, Int> = TODO()
12+
fun Map<String, Int>.merge(map: Map<String, Int>, operation: (Int, Int) -> Int): Map<String, Int> =
13+
(keys + map.keys).associateWith { key ->
14+
if (this[key] != null && map[key] != null) {
15+
operation(this[key]!!, map[key]!!)
16+
} else {
17+
this[key] ?: map[key]!!
18+
}
19+
}

0 commit comments

Comments
 (0)