compactMap and flatMap: Clean Up Optionals and Crashes!
HTML-код
- Опубликовано: 10 фев 2025
- Both of these functions are super useful for reducing the nil checking in your programs. In this lesson I'll go over a couple of examples as to how and when to effectively integrate these functions into the logic of your applications. Enjoy.
AppStore JSON APIs
www.letsbuildt...
Tinder Firestore Swipe and Match
www.letsbuildt...
Twitter Slide Out Menu Course
www.letsbuildt...
Podcasts Course
www.letsbuildt...
Intermediate Training Core Data
www.letsbuildt...
Instagram Firebase Course
www.letsbuildt...
Facebook Group
/ 1240636442694543
Completed Source Code
www.letsbuildt...
Instagram: / buildthatapp
Twitter: / buildthatapp
Hackintosh Build
i9 9900k: amzn.to/2CvQj6v
ASUS Z390 Motherboard: amzn.to/2JpaQ2B
16GB Corsair Vengeance RGB RAM: amzn.to/2YbygvJ
Corsair H100i Platinum Water Cooler: amzn.to/2FnbM22
Thermaltake RGB Power Supply: amzn.to/2TgKDTu
Intel 1TB SSD: amzn.to/2Tkdk1V
Corsair 280x Computer Case: amzn.to/2WcjvH2
AMD Radeon 4GB RAM GFX Card: amzn.to/2TYMyAp
LG 34" Widescreen 3440 x 1440: amzn.to/2Tk7q0S
Rode NT1 Microphone: amzn.to/2TZgX1r
Focusrite Audio Interface: amzn.to/2U12gey
Panasonic GH4 DSLR: amzn.to/2U4BrG6
Magewell HDMI to USB3 Capture Card: amzn.to/2Jp2esI
map applies a transformation on each element before appending the result of that transformation to an output array.
flatMap applies the transformation to the element, and concatenates the result of that transformation to each subsequent transformation.
Thanks for the information
Hey Brian. Fantastic stuff as always. I'm ready to download the app if you could put all your videos togather in. :)
Good video bro
grind never stops
from 2021 and thanks!
Cool video bro
Thank you! :)
When you use compactMap and then flatMap, is it not slow when you have a lot of data?
exactly
It's just a simple iteration over an array, how would it be different from a for loop?
Is there any other solution to do that??
Not sure what you mean by slow, as it is still O(n) time, which will hold true regardless of how you iterate through the elements.
However, if you're concerned with processing each result as a stream of elements to be generated only when you need the element, you can look into .lazy . Lazy generation allows you to process elements downstream before continuing to process the subsequent elements in your stream. Do note that this will still be a sequential operation, but depending on your scenario it can be useful.
Thanks Brain
hey brian,please make another firebase backend bu with kotlin
Didn't I do that already? Am I going crazy in 2019?
Please can u help me how to watch ur content on ur RUclips channel
From the noob part to the pro part
Because i have just stared learning xcode and swift
Thanks
You’re probably on the wrong channel if you want newbie friendly content
@@LetsBuildThatApp but i need ur guidance any how
Hatur nuhun bro
s.flatMap(transform) is equivalent to Array(s.map(transform).joined()).
flatMap is significantly cleaner no?
Can you please explain defer?
Brian Voong!!!
I didn't know compactMap handles optionals for you. That's awesome, but having to chain the mapping functions doesn't seem ideal since you have to make another pass over the array. I know this is just a simple example lol
I feel like there's probably a better way around this issue that I'm not aware of but overall I like using this approach if it removes the code of nil checking.
🎉👍🏻
Second to comment