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

Michel Fortin michel.fortin at michelf.ca
Sat Nov 11 10:49:03 CST 2017


> 	• What is your evaluation of the proposal?

One thing to keep in mind is that flatMap is basically the equivalent of optional chaining for non-`self` parameters.

	let result = character?.hexValue // optional chaining
	let result = character.flatMap { hexValue($0) } // optional "calling"?

I always found that `flatMap` was a weird name for this. Moreover, if the function you call is not returning an optional, `map` and `flatMap` essentially become synonyms. And thus I find myself using `flatMap` all the time when writing code like this, even when `map` would be enough, simply because it does what I expect all the time (like optional chaining, and unlike `map` that will instead pile up two layers of optionals).

The name `filterMap` makes a lot of sense for arrays, but it's still a bit awkward for optionals. I think the reason is that the most common thing you'd want to do with an optional is what `flatMap`/`filterMap` does, but the less specialized name (`map`) is already reserved with slightly different semantics. That is a bit dissonant in usage, even though the semantics makes sense in theory.

I think the most common use case, the one that mirrors optional chaining, should use the simplest name. I'm not sure what that word would be though.

[[
Side note: If only there was a nice way to express optional calling (when one of the parameters needs to be unwrapped or the call is skipped) like there is for optional chaining... I dream of this:

	let result = optional hexValue(character?)

where `optional` before an expression enables explicit unwrapping in subexpressions with `?` and any failure to unwrap a subexpression causes the optional expression to return `nil`. But that might be asking for too much sugar.
]]


> 	• Is the problem being addressed significant enough to warrant a change to Swift?

I actually implemented `Sequence.filterMap` and `Optional.filterMap` in one of my projects and attempted to replace every `flatMap` with it. Looks like most of the `flatMap` calls in my project are used to filter optionals in arrays or doing optional calling. Only a tiny amount had to keep using `flatMap` because they were flattening nested arrays. That leads me to the conclusion that a better name would make most of the code more readable.

I think the change is worth it for arrays. Flattening an array is not the same thing as removing parts of it, so the word "filter" adds clarity.

I'm not sure it adds any clarity for optionals though.


> 	• Does this proposal fit well with the feel and direction of Swift?

I think it fits well for arrays. Not sure about optionals.


> 	• If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

N/A


> 	• How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

I implemented filterMap to see what it'd feel like.


-- 
Michel Fortin
https://michelf.ca

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171111/c9428b60/attachment.html>


More information about the swift-evolution mailing list