Created
February 23, 2017 06:02
-
-
Save animatedlew/5f91561c6733dc46087e06e05990c799 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 main extends App { | |
def perm[T](xs: List[T]): Seq[List[T]] = { | |
val len = xs.size | |
if (len < 2) List(xs) | |
else | |
(0 until len).flatMap { i => | |
val (head, tail) = (xs(i), xs.take(i) ++ xs.drop(i + 1)) | |
perm(tail).foldLeft(List[List[T]]())((r, p) => (head :: p) :: r) | |
} | |
} | |
println(s"Permutation: ${perm(List('a', 'b', 'c'))}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment