I've been using Memo
from scalaz for a while, however, here is the situation that I feel I am not able to remain pure
.:
def compute(a: Int, b: Int): Int = {a+b} //an expensive computation
val cache = Memo.immutableHashMapMemo[(Int, Int), Int]{
case ((a,b)) => compute(a,b)
}
Now, I have s1
and s2
both in type Set[(Int, Int)]
. For example, s1 = Set((1,1), (1,2))
and s2 = Set((1,2), (1,3))
. Each list has to be run in parallel:
def computePar(s: Set[(Int, Int)]): Set[Int] = //using compute() in parallel
So the issue is each time I can only get the list of the results from an input list. While my Memo
should still be Map[(Int, Int), Int]
because you can reuse compute(1,2)
from s1
for the first element in s2
. Using a mutable map should solve the problem. I just wonder if there is a FP solution. I feel it might be related to Kleisli
or similar.