> On Feb 13, 2017, at 4:40 PM, Maxim Veksler via swift-users > <swift-users@swift.org> wrote: > > I'm trying to understand the signature of flapMap that allows it to "strip > down" Optional from a type, when called on an Optional collection but remain > same type when called on a non option collection. > > How would flatMap be implemented if would have been stand alone? > > This for ex. I can't get to compile: > > func flatMap<ElementOfResult, Element, T: Sequence>(on: T, _ transform: > (Element) throws -> ElementOfResult? > ) rethrows -> [ElementOfResult] { > var result: [ElementOfResult] = [] > for element in on { > if let newElement = try transform(element) { > result.append(newElement) > } > } > return result > }
This doesn’t compile because there’s no relation between T.Element.Iterator and Element. I think you want to remove the Element type parameter altogether, and make the input type of flatMap into T.Element.Iterator. Slava > > > Additionally please see 2 related SO questions, for background. > > http://stackoverflow.com/q/42213656 <http://stackoverflow.com/q/42213656> > http://stackoverflow.com/q/42214880 <http://stackoverflow.com/q/42214880> > _______________________________________________ > swift-users mailing list > swift-users@swift.org > https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users