【发布时间】:2017-01-17 22:57:13
【问题描述】:
我已经使用 scalaz 的 Memo 有一段时间了,但是,我觉得我无法继续使用 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)
}
现在,我有 s1 和 s2 类型为 Set[(Int, Int)]。例如,s1 = Set((1,1), (1,2)) 和 s2 = Set((1,2), (1,3))。每个列表都必须并行运行:
def computePar(s: Set[(Int, Int)]): Set[Int] = //using compute() in parallel
所以问题是每次我只能从输入列表中获取结果列表。虽然我的Memo 仍然应该是Map[(Int, Int), Int],因为您可以将s1 中的compute(1,2) 重复用于s2 中的第一个元素。使用可变映射应该可以解决问题。我只是想知道是否有FP解决方案。我觉得它可能与Kleisli 或类似的有关。
【问题讨论】:
标签: functional-programming scalaz