Why doesn’t compactMap() exist?

It's totally possible to implement compact() as shown e.g. in SE-0218 — Introduce compactMapValues to Dictionary - #20 by pyrtsa.

But in absense of parameterised generics (something like extension<T> Sequence where Element == T? { ... }), I think overeager autocompletion might still be the issue preventing the adoption of this somewhat hacky implementation in the stdlib; we don't want to suggest xs.compact() when xs is not a sequence of optionals.

2 Likes