[swift-evolution] [draft] Introduce Sequence.filteredMap(_:)
Max Moiseev
moiseev at apple.com
Wed Oct 25 13:05:59 CDT 2017
> On Oct 25, 2017, at 2:15 AM, Tino <2th at gmx.de> wrote:
>
>
>>> It’s unfortunate when a piece of code gets a different meaning — but as you said:
>>> It’s a misuse, and when you write code that is wrong, the results might be wrong as well.
>> And this is exactly that we’re trying to address. Not the impurity of the function, not the performance, but the potential to misuse.
> But do you think filterMap can’t be misused? I don’t know why people choose flatMap instead of map, but they might as well just switch to filterMap.
Oh I’m sure they will. But I hope it will be harder, because we will have a deprecated overload of a flatMap that will provide a helpful message and guide people to use map instead.
>
>>> Maybe Scala chose better name for the concept, but the Swift definition is consistent as it is now.
>> collect does not say anything about the transformation (A => B) that happens along the way…
> „collectResults“ is probably clearer… but I guess Scala-folks choose to prefer a single word.
Let’s not forget about the types. Function type is an essential piece of information that should accompany it’s name. With a function type being (LIst[A], PartialFunction[A, B]) => List[B] the name is almost not needed, there is pretty much one way of implementing this function (if we forget about trivial and wrong implementations that, for example, return an empty list all the time, of perform arbitrary side-effects).
> But when it’s about clear naming, we probably should talk about „applyFunctionAndCollectUnwrappedResults“, because filtering has no obvious connection to nil-values.
> Even worse, I think filter is the wrong word in this context, because that’s what I’d expect from such a function:
> Sequence.filterMap<U>(_: (Element) -> U?) -> [U?]
>
> In this case, the result would only contain those Optionals that contain a value — or there could be a parameter for the filter, defaulted to a check for nil.
>
>>> So, if you remove the third overload, the second one imho should be renamed, too.
>> Honestly, I don’t see why. Can you elaborate? The second one is perfectly consistent, it does not mix two different functor instances in the same function.
> As it is now, both Sequence and Optional are very similar — they are container types that may be empty.
> When you say that one incarnation of flatMap that deals with Optionals is wrong, imho it’s cleaner to break that connection completely, and make it a Collection-only thing.
> Overload 2 is kind of a special case of the third one.
I was actually thinking in the opposite direction ;-) As in, adding more functions from Collection to Optional. filter can be useful, count, isEmpty, forEach… I’m not saying that it will conform to the Collection protocol, but I can see how sometimes `opt.filter { $0 > 0 }` is a better option than `if let v = opt, x > 0 { … } else { … }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171025/e14dcbee/attachment.html>
More information about the swift-evolution
mailing list