com.uncarved.helpers

object Memoize

[source: com/uncarved/helpers/Memoize.scala]

object Memoize
extends AnyRef
Factory object for producing generalized memoized functions of up to four arguments. Usage:
  val circArea = 
      Memoize((r:Double)=>Math.Pi * Math.pow(r,2))

  val area = circArea(2)

  val cylVol = Memoize {
      (r:Double, h:Double) => 
        val vol = Math.Pi * Math.pow(r,2) * h
        println("Radius: " + r + " Height: " + 
            h + " Volume: " + vol)
        vol
  }

  val vol = cylVol(2, 3.5)
  val twoRCyl = cylVol.curry(2) //We can partially apply as normal
  val vol2 = twoRCyl(3.5)       //...and the memoization cache is shared

  def sphereVol(r: Double) = Math.Pi * Math.pow(r,3)

  val memoSV = Memoize(sphereVol _)
  
See Also
http://www.itl.nist.gov/div897/sqg/dads/HTML/memoize.html
http://www.uncarved.com/blog/memoization.mrk
Method Summary
def apply [T, R](f : (T) => R) : MemoizedFunction1[T, R]
def apply [T1, T2, T3, R](f : (T1, T2, T3) => R) : MemoizedFunction3[T1, T2, T3, R]
def apply [T1, T2, T3, T4, R](f : (T1, T2, T3, T4) => R) : MemoizedFunction4[T1, T2, T3, T4, R]
def apply [T1, T2, R](f : (T1, T2) => R) : MemoizedFunction2[T1, T2, R]
Methods inherited from AnyRef
getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Class Summary
class MemoizedFunction1 [-T, +R](f : (T) => R) extends (T) => R
A memoized single-argument function
class MemoizedFunction2 [-T1, -T2, +R](f : (T1, T2) => R) extends (T1, T2) => R
A memoized two-argument function
class MemoizedFunction3 [-T1, -T2, -T3, +R](f : (T1, T2, T3) => R) extends (T1, T2, T3) => R
A memoized three-argument function
class MemoizedFunction4 [-T1, -T2, -T3, -T4, +R](f : (T1, T2, T3, T4) => R) extends (T1, T2, T3, T4) => R
A memoized four-argument function
Method Details
def apply[T, R](f : (T) => R) : MemoizedFunction1[T, R]

def apply[T1, T2, R](f : (T1, T2) => R) : MemoizedFunction2[T1, T2, R]

def apply[T1, T2, T3, R](f : (T1, T2, T3) => R) : MemoizedFunction3[T1, T2, T3, R]

def apply[T1, T2, T3, T4, R](f : (T1, T2, T3, T4) => R) : MemoizedFunction4[T1, T2, T3, T4, R]