Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Created February 23, 2017 06:02
Show Gist options
  • Save animatedlew/5f91561c6733dc46087e06e05990c799 to your computer and use it in GitHub Desktop.
Save animatedlew/5f91561c6733dc46087e06e05990c799 to your computer and use it in GitHub Desktop.
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