Skip to content

Instantly share code, notes, and snippets.

@oisdk

oisdk/multiMaps.Swift

Last active Aug 29, 2015
Embed
What would you like to do?
extension SequenceType where Generator.Element: SequenceType {
var transposeAndPad: [[Generator.Element.Generator.Element]]? {
var genGen = self.generate()
let first = genGen.next()
var gens = AnySequence{genGen}.map{$0.generate()}
return first.map { $0.map { leftEl in [leftEl] + gens.indices.map{gens[$0].next() ?? leftEl} }
}
}
var transpose: [[Generator.Element.Generator.Element]] {
var gens = self.map{$0.generate()}
var ans: [[Generator.Element.Generator.Element]] = []
for
var toAp: [Generator.Element.Generator.Element] = [];;
ans.append(toAp), toAp.removeAll(keepCapacity: true) {
for ind in gens.indices {
if let element = gens[ind].next() {
toAp.append(element)
} else {
return ans
}
}
}
}
}
extension CollectionType where Generator.Element: CollectionType {
var unsafeTranspose: [[Generator.Element.Generator.Element]] {
return self.first!.indices.map{ ind in self.map{ $0[ind] } }
}
}
extension SequenceType {
func perMap
<S: SequenceType, T where S.Generator.Element == (Generator.Element -> T)>
(transforms: S) -> [[T]] {
return self.map{element in transforms.map{$0(element)}}
}
func multiMap
<S: SequenceType, T where S.Generator.Element == (Generator.Element -> T)>
(transforms: S) -> [[T]] {
return transforms.map{self.map($0)}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.