scala - Functional value, mutable state and tread-safety -
is trying to present in the recursion algorithm. Case Class Memo [A, B] (F: A = & gt; B) Expands (A = & gt; B) {Personal Val Cache = Mutable. Map Criterion [A, B] DEF (X: A) = gets the cash. X, f (x))} Private Val fib: memo [int, biggte] = memo {case 0 = & gt; 0 matter 1 = & gt; 1 case n = & gt; FIB (N-1) + FIB (N-2) DEP EF (N: IIT) = {FIB (N)}
This means that if we have unstable state and functional Use value (Val defined by. Well well functional value) So this thread is not safe?
( val fib - global scoped looks like mutable variable / object)
I would say yes, if you are calling more thread foo at the same time then there may be a problem, though I was thinking that if you were only adding to the map And always have the same value for the key, so there is no guarantee that it should work. If the implementation of the getOrElseUpdate method of the map is for any reason, then it will not work, the temporary map is not based on the contract definition.
If you can, you can add synchronization to your map and you are okay:
Private Val cache = new synchronized map [A, B] [A, B]
Comments
Post a Comment