Trying to introduce memoization into the recursion algorithm.
case class Memo[A,B](f: A => B) extends (A => B) {
private val cache = mutable.Map.empty[A, B]
def apply(x: A) = cache getOrElseUpdate (x, f(x))
}
private val fib: Memo[Int, BigInt] = Memo {
case 0 => 0
case 1 => 1
case n => fib(n-1) + fib(n-2)
}
def foo(n: Int) = {
fib(n)
}
Does it mean that if we use mutable state and functional value (defined by val.. well almost functional value) then it is not thread-safe?
(val fib
- looks like global scoped mutable variable/object)