I'll argue in favor of names which include the term "some". "The Optional type is an enumeration with two cases. Optional.none is equivalent to the nil literal. Optional.some(Wrapped) stores a wrapped value." (https://developer.apple.com/documentation/swift/optional)
let things = ["apple", nil, "cat", "dog"] print( things ) print( things.flatMap {$0} ) // Equivalently: apply map, filter for non-nil, and apply force unwrap. func transform(x: Any?) -> Any? {return x} print( things.map(transform).filter { $0 != nil }.map { $0! } ) for x in things { switch x { case .some: print(x!) case .none: break } } [Optional("apple"), nil, Optional("cat"), Optional("dog")] > ["apple", "cat", "dog"] > ["apple", "cat", "dog"] > apple > cat > dog Here's a variety of possible new names, in descending order of personal preference: mapUnwrappingSome mapAndUnwrap // Thanks Nevin, this is surprisingly clear. mapUnwrapSome mapUnwrapIfSome mapSome // For these last three, it's unclear when nil elements are dropped. Before or after the map? mapNonNil mapStrippingNil I'm opposed to filterMap. Combining the names of two commonly used methods for this less frequent operation seems likely to cause as much confusion as flatMap. I'm opposed to compactMap. There are two English meanings of "compact", and my initial thought was about enforcement of some kind of contract. I think mapUnwrappingSome is explicit, puts clarity > brevity, and maintains naming consistency within Swift. If the enumeration cases .some and .none are taught along with Optionals, then this name is clear. That's my 2-cents as a relatively inexperienced user. -Dylan On Wed, Nov 15, 2017 at 6:48 PM, Nevin Brackett-Rozinsky via swift-evolution <swift-evolution@swift.org> wrote: > On Wed, Nov 15, 2017 at 8:05 PM, Wallacy via swift-evolution < > swift-evolution@swift.org> wrote: > >> “unwrappingMap”(or some variations using unwrap). >> > > I’d like to propose “mapAndUnwrap”. > > It does what it says on the tin: map a sequence (into an optional type), > then unwrap the values which exist. > > Nevin > > _______________________________________________ > swift-evolution mailing list > swift-evolution@swift.org > https://lists.swift.org/mailman/listinfo/swift-evolution > >
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution