[swift-evolution] [Review] SE-0187: Introduce Sequence.filterMap(_:)

BJ Homer bjhomer at gmail.com
Tue Nov 14 11:41:50 CST 2017


“Flatten”, in the context here (flattening a Sequence of Optionals), only makes sense if you consider Optional as a sequence of zero or one elements. While such a sequence does act very similarly to an Optional, the fact remains that Optional is not a Sequence, and is generally not treated as one by any other part of the language. It fundamentally makes no more sense to “flatten" a Sequence of Optionals than it does to “flatten" an Optional of a Sequence.

In my mind, filter makes sense because I’m filtering out some of the results of the transformation operation before they get put into the resulting array. It’s a different kind of operation; not a map, but a filterMap. (Maybe filteringMap would be clearer?) The operation is not “map into a new array, then filter”. It’s “run this transformation for each item. If it produces something, keep it.” There’s no filter or compact that runs on the entire collection.

-BJ





> On Nov 14, 2017, at 7:50 AM, Tino Heth via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> Yeah but it seems clear from the return type so I am not sure that much confusion would really exist.
> 
> 
> Afaics, there already is lots of confusion — that’s the reason for me to write a sequence of posts in this topic, instead of an Optional ;-)
> The word „flatten“ is a quite honest description, so I wonder why words like filter, remove, ignoring or skipping should be used instead.
> 
> „Compact“ would be less irritating, but I could imagine that it indicates something like eliminating repeated occurrences.
> 
> Tino
> 
> (I’m quite close to attach a hand-drawn illustration of the flatMap process — I don’t think you want that to happen ;-)
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171114/809f8cbb/attachment.html>


More information about the swift-evolution mailing list