-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions that take a function and record the input-output values and cache them. There are options for one, two, or three argument functions, as well as the same with a set of default values for the memoization cache
- Loading branch information
1 parent
077f28b
commit 0a230cf
Showing
2 changed files
with
117 additions
and
13 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/main/kotlin/com/geistindersh/aoc/helper/caching/Memoize.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.geistindersh.aoc.helper.caching | ||
|
||
/** | ||
* Generate a function that will keep track of the input and output of the function | ||
* and cache the results. | ||
* Repeated calls with the same arguments will return the same result, and [fn] should | ||
* be free of side effects. | ||
* | ||
* @param fn A function to memoize | ||
* @return A function that takes the same arguments as the memoized function | ||
*/ | ||
@Suppress("unused") | ||
fun <A, Z> memoize(fn: (A) -> Z): (A) -> Z { | ||
val memory = mutableMapOf<A, Z>() | ||
return { memory.getOrPut(it) { fn(it) } } | ||
} | ||
|
||
/** | ||
* Generate a function that will keep track of the input and output of the function | ||
* and cache the results. | ||
* Repeated calls with the same arguments will return the same result, and [fn] should | ||
* be free of side effects. | ||
* | ||
* @param fn A function to memoize | ||
* @param defaultValues Values to pre-populate the input-output cache with | ||
* @return A function that takes the same arguments as the memoized function | ||
*/ | ||
@Suppress("unused") | ||
fun <A, Z> memoize( | ||
fn: (A) -> Z, | ||
defaultValues: Map<A, Z>, | ||
): (A) -> Z { | ||
val memory = defaultValues.toMutableMap() | ||
return { memory.getOrPut(it) { fn(it) } } | ||
} | ||
|
||
/** | ||
* Generate a function that will keep track of the input and output of the function | ||
* and cache the results. | ||
* Repeated calls with the same arguments will return the same result, and [fn] should | ||
* be free of side effects. | ||
* | ||
* @param fn A function to memoize | ||
* @return A function that takes the same arguments as the memoized function | ||
*/ | ||
@Suppress("unused") | ||
fun <A, B, Z> memoize(fn: (A, B) -> Z): (A, B) -> Z { | ||
val memory = mutableMapOf<Pair<A, B>, Z>() | ||
return { a, b -> memory.getOrPut(a to b) { fn(a, b) } } | ||
} | ||
|
||
/** | ||
* Generate a function that will keep track of the input and output of the function | ||
* and cache the results. | ||
* Repeated calls with the same arguments will return the same result, and [fn] should | ||
* be free of side effects. | ||
* | ||
* @param fn A function to memoize | ||
* @param defaultValues Values to pre-populate the input-output cache with | ||
* @return A function that takes the same arguments as the memoized function | ||
*/ | ||
@Suppress("unused") | ||
fun <A, B, Z> memoize( | ||
fn: (A, B) -> Z, | ||
defaultValues: Map<Pair<A, B>, Z>, | ||
): (A, B) -> Z { | ||
val memory = defaultValues.toMutableMap() | ||
return { a, b -> memory.getOrPut(a to b) { fn(a, b) } } | ||
} | ||
|
||
/** | ||
* Generate a function that will keep track of the input and output of the function | ||
* and cache the results. | ||
* Repeated calls with the same arguments will return the same result, and [fn] should | ||
* be free of side effects. | ||
* | ||
* @param fn A function to memoize | ||
* @return A function that takes the same arguments as the memoized function | ||
*/ | ||
@Suppress("unused") | ||
fun <A, B, C, Z> memoize(fn: (A, B, C) -> Z): (A, B, C) -> Z { | ||
val memory = mutableMapOf<Triple<A, B, C>, Z>() | ||
return { a, b, c -> memory.getOrPut(Triple(a, b, c)) { fn(a, b, c) } } | ||
} | ||
|
||
/** | ||
* Generate a function that will keep track of the input and output of the function | ||
* and cache the results. | ||
* Repeated calls with the same arguments will return the same result, and [fn] should | ||
* be free of side effects. | ||
* @param fn A function to memoize | ||
* @param defaultValues Values to pre-populate the input-output cache with | ||
* | ||
* @return A function that takes the same arguments as the memoized function | ||
*/ | ||
@Suppress("unused") | ||
fun <A, B, C, Z> memoize( | ||
fn: (A, B, C) -> Z, | ||
defaultValues: Map<Triple<A, B, C>, Z>, | ||
): (A, B, C) -> Z { | ||
val memory = defaultValues.toMutableMap() | ||
return { a, b, c -> memory.getOrPut(Triple(a, b, c)) { fn(a, b, c) } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters