Created
May 28, 2015 15:02
-
-
Save joshlemer/18972d7696a793bfc154 to your computer and use it in GitHub Desktop.
Lazy Evaluate all permutations of a String in Scala
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
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