[swift-evolution] [Accepted and Focused Re-review] SE-0187: Introduce Sequence.filterMap(_:)
swift at davedelong.com
Thu Nov 16 11:44:32 CST 2017
I agree with Gwendal’s reasoning about the introduction of the term “compact” into the API nomenclature. Removing non-existent values is qualitatively different than selecting a subset of a sequence based on properties of the contained values, and thus merits different terminology. Put a different way: A “filter" shouldn’t change the type of the sequence’s element.
In the case of filterMap(), we have two type transformations going on: T → U? and U? → U. Neither of these is a filter, because neither is an “A → A” transformation. So using the term “filter” is incorrect. The first is a map(), and the second is technically a reduce(). However, reduce() isn’t commonly used in Swift, and the special circumstances surrounding the use of Optional<U>, IMO, deserves a special term, given the frequency with which optionals are used. “Compact” fits that bill very nicely.
Therefore, a huge +1 from me on the choice of “compacted()/compacting()/compactMap()" over “filterMap” or whatever.
More information about the swift-evolution