My Awesome Newsletter

Archives
April 7, 2020

How to Flatten an Optional in Swift with flatMap 🔨

# **flatMap, Double Optionals, and Functional Programming** A while ago [I did an article on the basics of **Optionals**](https://learningswift.brightdigit.com/understanding-optionals-in-swift/) and how they work in Swift. However, it seems there’s some confusion as to how one particular method works in Swift when it comes to a collection of **Optionals**: ***flatMap***.  With all the talk regarding *flattening the curve*, today I want to explain exactly what it means to flatten in the context of functional programming. Additionally, it not only can be used on **Arrays** but single **Optionals** as well. In the end, using them correctly will make your code easier to read. **In this article, I talk about:** - What flatten means in functional programming - How to use flatMap to reduce *double optionals* (i.e. *String??*) - When to use compactMap vs flatMap **Read more here...** Thanks,
Leo
Don't miss what's next. Subscribe to My Awesome Newsletter:
Powered by Buttondown, the easiest way to start and grow your newsletter.