Skip to content

Instantly share code, notes, and snippets.

@netanelrabinowitz
Created April 10, 2018 21:34
Show Gist options
  • Select an option

  • Save netanelrabinowitz/6d81e1e9112f10274b9779179ce690dd to your computer and use it in GitHub Desktop.

Select an option

Save netanelrabinowitz/6d81e1e9112f10274b9779179ce690dd to your computer and use it in GitHub Desktop.
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