[swift-users] How flatMap's signature is able to translate from Optional<Type> to [Type]?

Maxim Veksler maxim at vekslers.org
Mon Feb 13 18:40:55 CST 2017

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) {




    return result


Additionally please see 2 related SO questions, for background.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170214/0ce622c0/attachment.html>

More information about the swift-users mailing list