是否有通用的方法来记忆Scala?

时间:2013-04-27 22:11:07

标签: scala scope dynamic-programming memoization forward-reference

我想记住这个:

def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2)
println(fib(100)) // times out

所以我写了这个,这令人惊讶地编译和工作(我很惊讶,因为fib在其声明中引用了自己:)

case class Memo[A,B](f: A => B) extends (A => B) {
  private val cache = mutable.Map.empty[A, B]
  def apply(x: A) = cache getOrElseUpdate (x, f(x))
}

val fib: Memo[Int, BigInt] = Memo {
  case 0 => 0
  case 1 => 1
  case n => fib(n-1) + fib(n-2) 
}

println(fib(100))     // prints 100th fibonacci number instantly

但是当我尝试在def内声明fib时,我收到编译器错误:

def foo(n: Int) = {
  val fib: Memo[Int, BigInt] = Memo {
    case 0 => 0
    case 1 => 1
    case n => fib(n-1) + fib(n-2) 
  }
  fib(n)
} 

以上无法编译error: forward reference extends over definition of value fib case n => fib(n-1) + fib(n-2)

为什么在def中声明val fib失败但在类/对象范围之外有效?

澄清一下,为什么我可能想在def范围内声明递归memoized函数 - 这是我对子集求和问题的解决方案:

/**
   * Subset sum algorithm - can we achieve sum t using elements from s?
   *
   * @param s set of integers
   * @param t target
   * @return true iff there exists a subset of s that sums to t
   */
  def subsetSum(s: Seq[Int], t: Int): Boolean = {
    val max = s.scanLeft(0)((sum, i) => (sum + i) max sum)  //max(i) =  largest sum achievable from first i elements
    val min = s.scanLeft(0)((sum, i) => (sum + i) min sum)  //min(i) = smallest sum achievable from first i elements

    val dp: Memo[(Int, Int), Boolean] = Memo {         // dp(i,x) = can we achieve x using the first i elements?
      case (_, 0) => true        // 0 can always be achieved using empty set
      case (0, _) => false       // if empty set, non-zero cannot be achieved
      case (i, x) if min(i) <= x && x <= max(i) => dp(i-1, x - s(i-1)) || dp(i-1, x)  // try with/without s(i-1)
      case _ => false            // outside range otherwise
    }

    dp(s.length, t)
  }

5 个答案:

答案 0 :(得分:33)

我找到了一种更好的使用Scala进行记忆的方法:

$ sudo umount /mnt
$ sudo losetup -d /dev/loop1

现在你可以按如下方式写下斐波纳契语:

def memoize[I, O](f: I => O): I => O = new mutable.HashMap[I, O]() {
  override def apply(key: I) = getOrElseUpdate(key, f(key))
}

这是一个有多个参数的人(选择函数):

lazy val fib: Int => BigInt = memoize {
  case 0 => 0
  case 1 => 1
  case n => fib(n-1) + fib(n-2)
}

这是子集和问题:

lazy val c: ((Int, Int)) => BigInt = memoize {
  case (_, 0) => 1
  case (n, r) if r > n/2 => c(n, n - r)
  case (n, r) => c(n - 1, r - 1) + c(n - 1, r)
}

编辑:如下所述,这是一个线程安全的版本

// is there a subset of s which has sum = t
def isSubsetSumAchievable(s: Vector[Int], t: Int) = {
  // f is (i, j) => Boolean i.e. can the first i elements of s add up to j
  lazy val f: ((Int, Int)) => Boolean = memoize {
    case (_, 0) => true        // 0 can always be achieved using empty list
    case (0, _) => false       // we can never achieve non-zero if we have empty list
    case (i, j) => 
      val k = i - 1            // try the kth element
      f(k, j - s(k)) || f(k, j)
  }
  f(s.length, t)
}

答案 1 :(得分:19)

类/特征级别val编译为方法和私有变量的组合。因此允许递归定义。

另一方面,本地val只是常规变量,因此不允许递归定义。

顺便说一句,即使您定义的def有效,也不会达到预期效果。每次调用foo时,都会创建一个新的函数对象fib,它将拥有自己的支持映射。你应该做的是这个(如果你真的希望def成为你的公共接口):

private val fib: Memo[Int, BigInt] = Memo {
  case 0 => 0
  case 1 => 1
  case n => fib(n-1) + fib(n-2) 
}

def foo(n: Int) = {
  fib(n)
} 

答案 2 :(得分:8)

Scalaz有一个解决方案,为什么不重用它?

import scalaz.Memo
lazy val fib: Int => BigInt = Memo.mutableHashMapMemo {
  case 0 => 0
  case 1 => 1
  case n => fib(n-2) + fib(n-1)
}

您可以阅读有关memoization in Scalaz的更多信息。

答案 3 :(得分:0)

Mutable HashMap不是线程安全的。同样为基本条件定义case语句似乎是不必要的特殊处理,而Map可以加载初始值并传递给Memoizer。以下是Memoizer的签名,它接受备忘录(不可变映射)和公式并返回递归函数。

Memoizer看起来像

def memoize[I,O](memo: Map[I, O], formula: (I => O, I) => O): I => O

现在给出以下斐波纳契公式,

def fib(f: Int => Int, n: Int) = f(n-1) + f(n-2)
带有Memoizer的斐波纳契可以定义为

val fibonacci = memoize( Map(0 -> 0, 1 -> 1), fib)

其中上下文无关的通用Memoizer定义为

    def memoize[I, O](map: Map[I, O], formula: (I => O, I) => O): I => O = {
        var memo = map
        def recur(n: I): O = {
          if( memo contains n) {
            memo(n) 
          } else {
            val result = formula(recur, n)
            memo += (n -> result)
            result
          }
        }
        recur
      }

同样,对于阶乘,公式是

def fac(f: Int => Int, n: Int): Int = n * f(n-1)

和使用Memoizer的factorial是

val factorial = memoize( Map(0 -> 1, 1 -> 1), fac)

灵感:Memoization,道格拉斯·克罗克福德的Javascript好的第4章

答案 4 :(得分:-1)

ZIO#cached是一种在ZIO中进行记忆的方法