0

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.

huoenter
  • 512
  • 3
  • 16

1 Answers1

0

Your question may be similar to this one: Pimping scalaz Memo

You will find solution to your problem there i think, but i have something more to say about TrieMap(structure proposed in linked question). It scales well with number of threads (horizontal scaling is very good), but it has quite a big constant cost of operations.

Community
  • 1
  • 1
L.Lampart
  • 755
  • 5
  • 10