【问题标题】:Functional programming design: A case about Memo in scalaz函数式编程设计:scalaz中的Memo案例
【发布时间】: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)
}

现在,我有 s1s2 类型为 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


    【解决方案1】:

    您的问题可能与此类似:Pimping scalaz Memo

    我认为您会在那里找到问题的解决方案,但我对 TrieMap(链接问题中提出的结构)还有更多话要说。它可以随线程数很好地扩展(水平扩展非常好),但它具有相当大的恒定操作成本。

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 1970-01-01
      • 2011-01-11
      • 2020-02-20
      • 2018-08-06
      • 2013-01-26
      • 2017-05-31
      • 1970-01-01
      • 2011-10-23
      相关资源
      最近更新 更多