Last active
September 5, 2015 20:08
-
-
Save oisdk/3495fd1b867b680d3deb to your computer and use it in GitHub Desktop.
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
func cons<S : SequenceType>(h: S.Generator.Element)(s: S) -> [S.Generator.Element] { | |
return [h] + s | |
} | |
private extension GeneratorType where Element : CollectionType { | |
mutating private func product() -> [[Element.Generator.Element]] { | |
return next()?.lazy.map(cons).flatMap(product().lazy.map) ?? [[]] | |
} | |
} | |
extension SequenceType where Generator.Element : CollectionType { | |
func product() -> [[Generator.Element.Generator.Element]] { | |
var g = generate() | |
return g.product() | |
} | |
} | |
[[1, 2], [3, 4]].product() // [[1, 3], [1, 4], [2, 3], [2, 4]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment