The compactMap() function returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
Use this method to receive an array of non-optional values when your transformation produces an optional value.
Returns a new dictionary containing only the key-value pairs that have non-nil values as the result of transformation by the given closure.
Use this method to receive a dictionary with non-optional values when your transformation produces optional values.
Here is my own Swift compactMap() and compactMapValues() Functions Implementation, that can be used with Collections and Dictionaries respectively.
In this project I used extensions for Collections and Dictionaries for creating myCompactMap() and myCompactValues() functions respectively in there, which are my own implementations of "official" compactMap() and compactMapValues() functions. myCompactMap() and myCompactMapValues are declared as Generic.
You can see some examples in my code if something remains unclear to you.