title: Functional value, mutable state and thread-safety
body: <p><a href="https://stackoverflow.com/questions/16257378/is-there-a-generic-way-to-memoize-in-scala?answertab=active#tab-top">From there</a></p>
  
  <p>Trying to introduce <a href="http://en.wikipedia.org/wiki/Memoization" rel="nofollow noreferrer">memoization</a> into the recursion algorithm.</p>
  
  <pre><code>case class Memo[A,B](f: A =&gt; B) extends (A =&gt; 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 =&gt; 0
    case 1 =&gt; 1
    case n =&gt; fib(n-1) + fib(n-2) 
  }
  
  def foo(n: Int) = {
    fib(n)
  } 
  </code></pre>
  
  <p>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?  </p>
  
  <p>(<code>val fib</code> - looks like global scoped mutable variable/object)</p>