Skip to content

Commit 6faaf64

Browse files
committed
Fix memoization function
1 parent 1690c5a commit 6faaf64

File tree

1 file changed

+2
-2
lines changed
  • src/main/kotlin/lv/esupe/aoc/utils

1 file changed

+2
-2
lines changed

src/main/kotlin/lv/esupe/aoc/utils/Memo.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package lv.esupe.aoc.utils
22

33
fun <T, R> memoize(function: (T) -> R): (T) -> R {
44
val cache = mutableMapOf<T, R>()
5-
return { t -> cache.computeIfAbsent(t, function) }
5+
return { t -> cache.getOrPut(t) { function(t) } }
66
}
77

88
fun <T, R, V> memoize(function: (T, R) -> V): (T, R) -> V {
99
val cache = mutableMapOf<Pair<T, R>, V>()
10-
return { t, r -> cache.computeIfAbsent(t to r) { (t, r) -> function(t, r) } }
10+
return { t, r -> cache.getOrPut(t to r) { function(t, r) } }
1111
}

0 commit comments

Comments
 (0)