, , , , , , , .
import annotation.tailrec
def product[T](listOfLists: List[List[T]]): List[List[T]] = {
@tailrec def innerProduct[T](listOfLists: List[List[T]], accum: List[List[T]]): List[List[T]] =
listOfLists match {
case Nil => accum
case xs :: xss => innerProduct(xss, for (y <- xs; a <- accum) yield y :: a)
}
innerProduct(listOfLists.reverse, List(Nil))
}
:
scala> product(List(List(1,2),List(3,4)))
res0: List[List[Int]] = List(List(1, 3), List(1, 4), List(2, 3), List(2, 4))