Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active September 5, 2015 20:08
Show Gist options
  • Save oisdk/3495fd1b867b680d3deb to your computer and use it in GitHub Desktop.
Save oisdk/3495fd1b867b680d3deb to your computer and use it in GitHub Desktop.
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