Last active
December 9, 2015 20:37
-
-
Save mkhl/17c39107046a3d06945a to your computer and use it in GitHub Desktop.
Permutations of Sequences in Swift
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
func permutations<Sequence: SequenceType>(items: Sequence) -> [[Sequence.Generator.Element]] { | |
return items.reduce([[]]) { (perms, item) in | |
return perms.flatMap { perm in | |
return (0 ... perm.count).map { i in | |
var copy = perm | |
copy.insert(item, atIndex: i) | |
return copy | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment