You’re proposing a method called
compactMap() that doesn’t actually map anything, merely filters out nils.
compactMap had never existed, being composible instead from
map and some
sansNils, then you would just use
sansNils in isolation for this purpose.
The existence of
compactMap is a bit of a mistake, I think - one people largely accept because it’s still fairly practical. But making it worse would be, well, even worse.
Sidenote: I don’t like the use of the term
compact in any of this, because it’s way too ambiguous - it doesn’t say to me “removes
nil“ specifically. Why doesn’t it mean to reallocate the underlying memory as one contiguous block? Or switch the internal representation from a linked list to an array? Or resize the backing memory to exactly the size of the current contents? Or apply bzip2 compression? Or reduce the type of the collection’s contents to the smallest type that still holds all the values (e.g. Int -> Int8)? Or remove duplicates? All of those are forms of compaction. It seems it only exists because somehow it got established as convention - some kind of chicken-and-egg paradox.