[swift-evolution] [Accepted and Focused Re-review] SE-0187: Introduce Sequence.filterMap(_:)
Dylan Brown
djwbrown at gmail.com
Wed Nov 15 21:33:02 CST 2017
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 at swift.org> wrote:
> On Wed, Nov 15, 2017 at 8:05 PM, Wallacy via swift-evolution <
> swift-evolution at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171115/2e7e8ae2/attachment.html>
More information about the swift-evolution
mailing list