scala - 在Scala中使用哪种类型存储内存中的可变数据表?

标签 scala data-structures memoization scala-collections

每次调用一个函数时,如果尚未记住给定参数值集的结果,我想将结果放入内存表中。一列用于存储结果,另一列用于存储参数值。

我如何最好地实现这一点?参数是多种类型的,包括一些枚举。

在C#中,我通常会使用DataTable。 Scala中有等效的东西吗?

最佳答案

您可以使用mutable.Map[TupleN[A1, A2, ..., AN], R],或者如果需要考虑内存,则可以使用WeakHashMap [1]。下面的定义(基于michid's blog的备注代码建立)使您可以轻松地备注带有多个参数的函数。例如:

import Memoize._

def reallySlowFn(i: Int, s: String): Int = {
   Thread.sleep(3000)
   i + s.length
}

val memoizedSlowFn = memoize(reallySlowFn _)
memoizedSlowFn(1, "abc") // returns 4 after about 3 seconds
memoizedSlowFn(1, "abc") // returns 4 almost instantly

定义:
/**
 * A memoized unary function.
 *
 * @param f A unary function to memoize
 * @param [T] the argument type
 * @param [R] the return type
 */
class Memoize1[-T, +R](f: T => R) extends (T => R) {
   import scala.collection.mutable
   // map that stores (argument, result) pairs
   private[this] val vals = mutable.Map.empty[T, R]

   // Given an argument x, 
   //   If vals contains x return vals(x).
   //   Otherwise, update vals so that vals(x) == f(x) and return f(x).
   def apply(x: T): R = vals getOrElseUpdate (x, f(x))
}

object Memoize {
   /**
    * Memoize a unary (single-argument) function.
    *
    * @param f the unary function to memoize
    */
   def memoize[T, R](f: T => R): (T => R) = new Memoize1(f)

   /**
    * Memoize a binary (two-argument) function.
    * 
    * @param f the binary function to memoize
    * 
    * This works by turning a function that takes two arguments of type
    * T1 and T2 into a function that takes a single argument of type 
    * (T1, T2), memoizing that "tupled" function, then "untupling" the
    * memoized function.
    */
   def memoize[T1, T2, R](f: (T1, T2) => R): ((T1, T2) => R) = 
      Function.untupled(memoize(f.tupled))

   /**
    * Memoize a ternary (three-argument) function.
    *
    * @param f the ternary function to memoize
    */
   def memoize[T1, T2, T3, R](f: (T1, T2, T3) => R): ((T1, T2, T3) => R) =
      Function.untupled(memoize(f.tupled))

   // ... more memoize methods for higher-arity functions ...

   /**
    * Fixed-point combinator (for memoizing recursive functions).
    */
   def Y[T, R](f: (T => R) => T => R): (T => R) = {
      lazy val yf: (T => R) = memoize(f(yf)(_))
      yf
   }
}

使用定点组合器(Memoize.Y)可以记住递归函数:
val fib: BigInt => BigInt = {                         
   def fibRec(f: BigInt => BigInt)(n: BigInt): BigInt = {
      if (n == 0) 1 
      else if (n == 1) 1 
      else (f(n-1) + f(n-2))                           
   }                                                     
   Memoize.Y(fibRec)
}

[1] WeakHashMap不能很好地用作缓存。参见http://www.codeinstructions.com/2008/09/weakhashmap-is-not-cache-understanding.htmlthis related question

关于scala - 在Scala中使用哪种类型存储内存中的可变数据表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3640823/

相关文章:

scala - 为什么不应该只使用一个用例类?

scala - 比较 Slick 查询中的类型映射值

scala - 如何避免在 Spark 中广播大型查找表

java - 当添加一些随机元素时,PriorityQueue中的优先级如何确定?

c - 如果我们在链表中查找循环时改变慢指针和快指针的跳数会怎样?

javascript - underscore.js _.memoize() 在行动中的例子?

scala - 关于 java.lang.NoClassDefFoundError : Could not initialize class org. xerial.snappy.Snappy

c - 为什么会出现错误 "request for data element in something not a structure"?

javascript - 获取对象中所有项目组合的高效算法

performance - 在非整数键上有效地实现记忆化