In Scala, what does “extends (A => B)” on a case class mean?

前端 未结 5 1517
梦如初夏
梦如初夏 2021-01-02 12:52

In researching how to do Memoization in Scala, I\'ve found some code I didn\'t grok. I\'ve tried to look this particular \"thing\" up, but don\'t know by what to call it; i.

5条回答
  •  感动是毒
    2021-01-02 13:36

    I am the original author of doing memoization this way. You can see some sample usages in that same file. It also works really well when you want to memoize on multiple arguments too because of the way Scala unrolls tuples:

        /**
         * @return memoized function to calculate C(n,r) 
         * see http://mathworld.wolfram.com/BinomialCoefficient.html
         */
         val c: Memo[(Int, Int), BigInt] = Memo {
            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)
         }
         // note how I can invoke a memoized function on multiple args too
         val x = c(10, 3) 
    

提交回复
热议问题