[swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

Alejandro Martinez alexito4 at gmail.com
Thu Oct 26 14:40:47 CDT 2017


I wrote about this in 2015
http://alejandromp.com/blog/2015/6/24/swift-flatmap/ and forgot about
it.
I just get used to it now but it's still hard to explain to newcomers
why flatMap has this "special" behaviour on Swift.
I'm not an expert on functional programming but if this doesn't make
sense in the theory and it's just for convenience I agree we could
rename it. Still, if it has some reason to work like this (higher
kinded types?) and we expect to have them in an infinite time scale...
maybe we should hold on.

On Wed, Oct 25, 2017 at 7:06 PM, Max Moiseev via swift-evolution
<swift-evolution at swift.org> wrote:
>
>
> 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 { … }
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>



-- 
Alejandro Martinez
http://alejandromp.com


More information about the swift-evolution mailing list