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

Sam Warner sam.warner at domain.com.au
Wed Nov 8 17:32:36 CST 2017


I accept the motivation for this change, although I would mention that:
- in 2.5 years on a ~200k lines of Swift project
- we've seen a plenty of instances of `flatMap` used where `map` would have
been sufficient, but
- we've never burned time on tracking down the sort of compiler issue
described by the author in the Motivation. As I say, the argument is fair,
but I am questioning how frequently this problem occurs.

I can't speak to the previous attempts to solve this problem, but I'd add
my voice to anyone advocating solving this by having a compiler warning
when `flatMap` is used redundantly(?).

If this proposal were to be accepted, I'd also question the choice of
`filterMap` as a name. This name is presumably shorthand for
`filterNonesAndMap`, which conveniently ignores the following:
- `flatMap` may be used on a sequence-of-sequences to flatten our to a
single-dimensional sequence.
- `flatMap` may be used on other 'box' types, e.g. on an optional: `Optional
<String>("foo").flatMap { .some($0 + "bar") }` or on the common `Result`
type.

Any re-naming of one `flatMap` should take these other uses into account,
as they share conceptual details.

Thanks,

Sam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171108/2d2dc488/attachment.html>


More information about the swift-evolution mailing list