Skip to content

Instantly share code, notes, and snippets.

@joshlemer
Created May 28, 2015 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshlemer/18972d7696a793bfc154 to your computer and use it in GitHub Desktop.
Save joshlemer/18972d7696a793bfc154 to your computer and use it in GitHub Desktop.
Lazy Evaluate all permutations of a String in Scala
def permsStream(s: String): Stream[String] = {
def interleave(head: Char, tail: String) =
0 to tail.length map (i => tail.substring(0, i) + head + tail.substring(i))
if (s.length == 0) Stream("")
else permsStream(s.tail) flatMap (interleave(s.head, _))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment