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

Tino Heth 2th at gmx.de
Wed Nov 8 15:29:47 CST 2017



> Am 08.11.2017 um 21:25 schrieb John McCall <rjmccall at apple.com>:
> 
>> My point is: The proposed filterMap isn’t a combination of map and filter at all — or can you build it just out of map & filter, like flatMap is constructed from map & flatten?
> 
> You can built it out of map and filter:
> 
>   func filterMap<T>(_ fn: (Element) -> U?) -> [U] {
>     return map(fn).filter { $0 != nil }.map { $0! }
>   }
True, but it’s not what I asked for — and it should rather be called „mapFilterMapForceUnwrap“, shouldn’t it? ;-)
The thing is that people who don’t fully understand flatMap (those that are the target group for the proposal) probably just use it because „it does a transformation and filters out nil (and nil is evil)“.
Renaming the function to filterMap hardens that misconception:
As you sample shows, filterMap is not like flatMap — the latter is honest („flatten the result of map“) the other isn’t („filter the result of map“ only strips the nils, but doesn’t change the return type)

So I think it’s better to keep flatMap, or replace it with something that doesn’t pretend to be a plain filter.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171108/78d0b09a/attachment.html>


More information about the swift-evolution mailing list