[swift-evolution] [Review] SE-0187: Introduce Sequence.filterMap(_:)
Gwendal Roué
gwendal.roue at gmail.com
Sat Nov 11 08:35:14 CST 2017
Hello,
> • What is your evaluation of the proposal?
I'm neutral. My humble contribution is about the name of the method, since many reactions so far have a problem with the proposed filterMap name.
I'm found of Ruby's `compact` method. Its role is to filter out nils:
[1, 2, nil, 4].compact # => [1, 2 3]
This is not exactly what our discussed flatMap does, I know. Ruby's compact does not take a mapping closure. Swift's flatMap removes only one level of nils, as many contributors have reminded us. I know, I know.
I'd suggest `compactMap` as an alternative name, should `filterMap` find too much resistance:
[1, 2, nil, 4].compactMap { $0 } // [1, 2, 4]
["1", "foo"]..compactMap { Int($0) } // [1]
I'd even suggest adding `compact()` as a shorthand for `compactMap { $0 }`, since filtering out nils as a very common operation.
"Compact" has the advantage of being a short word. "Compacting" a collection would just mean removing its nils, a concept that could easily stick in developers' minds, as Ruby has shown.
> • Is the problem being addressed significant enough to warrant a change to Swift?
No. I too am guilty of having used flatMap when map was enough. Another name would not have changed this. It was just part of my learning phase.
Gwendal Roué
More information about the swift-evolution
mailing list