Scala Memoization: How does this Scala memo work?
The following code is from Pathikrit's Dynamic Programming repository. I'm mystified by both its beauty and peculiarity. def subsetSum(s: List[Int], t: Int) = { type DP = Memo[(List[Int], Int), (Int, Int), Seq[Seq[Int]]] implicit def encode(key: (List[Int], Int)) = (key._1.length, key._2) lazy val f: DP = Memo { case (Nil, 0) => Seq(Nil) case (Nil, _) => Nil case (a :: as, x) => (f(as, x - a) map {_ :+ a}) ++ f(as, x) } f(s, t) } The type Memo is implemented in another file: case class Memo[I <% K, K, O](f: I => O) extends (I => O) { import collection.mutable.{Map => Dict} val cache = Dict