Created
April 10, 2018 21:34
-
-
Save netanelrabinowitz/6d81e1e9112f10274b9779179ce690dd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| sealed abstract class Memo[K, V] { | |
| def apply(z: K => V): K => V | |
| } | |
| object Memo { | |
| def memo[K, V](f: (K => V) => K => V): Memo[K, V] = new Memo[K, V] { | |
| def apply(z: K => V) = f(z) | |
| } | |
| def mutableHashMapMemo[K, V]: Memo[K, V] = mutableMapMemo(new mutable.HashMap[K, V]) | |
| private def mutableMapMemo[K, V](a: mutable.Map[K, V]): Memo[K, V] = memo[K, V](f => k => a.getOrElseUpdate(k, f(k))) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment