RosettaCodeData/Task/Combinations/Scala/combinations-2.scala

9 lines
281 B
Scala

def combs[A](n: Int, l: List[A]): Iterator[List[A]] = n match {
case _ if n < 0 || l.lengthCompare(n) < 0 => Iterator.empty
case 0 => Iterator(List.empty)
case n => l.tails.flatMap({
case Nil => Nil
case x :: xs => combs(n - 1, xs).map(x :: _)
})
}