/Swift_CompactMap_and_CompactMapValues_Implementing

My Own Swift compactMap(_ :) and compactMapValues(_ :) Functions Implementing

Primary LanguageSwift

Swift compactMap(_ :) and compactMapValues(: _) Functions Implementing

Introduction

Using the compactMap() Function with Collections

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.

Using the compactMapValues() Function with Dictionaries

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.

About My Implementation

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.

References

Contacts