Last active
January 4, 2016 03:49
-
-
Save ajozwik/8564437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object S99_P26 { | |
def combinations[T](count: Int, ts: Seq[T]): Seq[Seq[T]] = { | |
def addHeadToSeqs[T](tss: Seq[Seq[T]], elt: T): Seq[Seq[T]] = | |
tss.map(ts => elt +: ts) | |
ts match { | |
case h +: tail if count == 1 => | |
ts.flatMap(e => Seq(Seq(e))) | |
case h +: tail => | |
combinations(count, tail) ++ addHeadToSeqs(combinations(count - 1, tail), h) | |
case _ => Nil | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for nice solution, I've made a simplified version.