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

John McCall rjmccall at apple.com
Wed Nov 8 14:25:04 CST 2017


> On Nov 8, 2017, at 3:20 PM, Tino Heth via swift-evolution <swift-evolution at swift.org> wrote:
>> This is a wonderful example! But it’s an argument for a different discussion (of general usefulness of implicit optional promotion). Thanks to the optional promotion, what the closure returns is not nil, but instead is .some(nil), and that is not filtered out.
> 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! }
  }

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171108/332d91fc/attachment.html>


More information about the swift-evolution mailing list