[swift-evolution] [draft] Introduce Sequence.filteredMap(_:)
moiseev at apple.com
Tue Oct 24 12:07:07 CDT 2017
> On Oct 24, 2017, at 5:10 AM, Tino <2th at gmx.de> wrote:
>> Calling "flatMap" a map + filtering out the nil values was a TERRIBLE idea.
> maybe I’m the one who’s getting this all wrong, but afaik the purpose of flatMap isn’t filtering out nil at all, but just a combination of map & flatten…
> Optional<T> is like Array<T> with a maximal capacity of one (so it can either contain a single object, or it’s empty), and in this interpretation, there’s nothing wrong with the behavior.
> Instead, I think Nobuo Saito is right, and that a renaming to filteredMap is only fighting symptoms, and not the cause (but I’m not that sure if there’s a good way to tackle the cause).
> Besides that, all those alternative names also have potential for confusion:
> Imho it isn’t intuitive what is filtered out — and don’t forget that the result of flatMap can contain nil elements…
> If the biggest problem of flatMap is that people who don’t understand it write code that isn’t optimal (but still does the right thing!), I don’t think there is any change needed. I’m not even sure that that wrapping/unwrapping is actually done, because it should be discoverable by the compiler.
It can be seen that the generated SIL is more complicated for a flatMap case, the microbenchmark also shows that it’s about 3x slower than the map. But even if that can be optimized away, don’t you find a String example from the proposal even a little convincing?
> It would be nice if there was an way to warn in places where flatMap could be replaced with map, though (but imho this special warning shouldn’t be checked by the compiler).
I tried to avoid renaming: https://github.com/apple/swift/pull/7823/ <https://github.com/apple/swift/pull/7823/> but then https://github.com/apple/swift/pull/9390 <https://github.com/apple/swift/pull/9390>.
> - Tino
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution