Core concept

val memo = collection.mutable.Map[Int, Int]()
def fib(n: Int): Int = memo.getOrElseUpdate(n,
  if (n < 2) n else fib(n - 1) + fib(n - 2))
Advertisement

How it works

Memory growth. Thread-safety for shared cache.

Advertisement

Trade-offs + gotchas

Expensive pure functions. Recursion optimization.