Extend flatMap to optional-valued transformations

Originator:jtbandes
Number:rdar://20561754 Date Originated:4/15/2015
Status:Closed Resolved:Fixed
Product:Developer Tools Product Version:
Classification:Enhancement Reproducible:N/A
 
The following variant of flatMap would be very very highly useful.

func flatMap<S : SequenceType, T>(source: S, @noescape transform: S.Generator.Element -> T?) -> [T]


Example implementation:

func flatMap<S : SequenceType, T>(source: S, @noescape transform: S.Generator.Element -> T?) -> [T] {
    var results: [T] = []
    for x in source {
        if let y = transform(x) {
            results.append(y)
        }
    }
    return results
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!