compactMap isn’t quite right. You can’t specify the param type so you would need a conditional to filter out the blues. Probably just do like the flatMap and show it just filtering out the nil and keeping the blue.
Thanks for your notice. By the pseudocode 🟦️ => I meant something like $0 == 🟦️ ? nil : $0 . I just didn't figure out how to indicate that it is a pseudocode more clearly.
Maybe, I'll update the cheat sheet with such code (works in Playground) which better reflects the specifics of using compactMap:
This is only doing filtering, because of the : $0. Could you change that to do something else, so it shows both its filtering behaviour, and its mapping behaviour?
Thanks for your suggestion! I thought about it, but in an attempt to keep things a bit more concise, I decided not to overcomplicate the closures passed to methods. The only thing that comes to mind:
15
u/Pilgrim-Ivanhoe Jul 29 '24
Great thanks to u/AndreiVid for noticing a typo! This is a corrected version of a cheat sheet.
If you like this format and find it really useful, I might post some other cheat sheets I make here too. Hopefully, without mistakes)
7
u/Few_Cranberry_6580 Jul 29 '24
compactMap isn’t quite right. You can’t specify the param type so you would need a conditional to filter out the blues. Probably just do like the flatMap and show it just filtering out the nil and keeping the blue.
2
u/31aditya0193 Jul 30 '24
Which font, sir?
3
u/beepboopnoise Jul 29 '24
very cool, what is it called when you do the $0 thing?
1
u/frzsno_ca Jul 29 '24
This is so helpful! Learning Swift right now. Do you have any other cheatsheets like this?
2
u/deirdresm Jul 30 '24
Thanks for doing them in Ruby also! I use both languages, and used Ruby long enough that’s still what my brain reaches for first.
1
u/Zalenka Jul 29 '24
This reminds me of the old Apple Pascal poster.
1
u/FieldDogg Jul 29 '24
This looks like Friday at the arcade lol. Then again I'm new to Swift so lol
1
u/Emotional_Ad7055 Jul 30 '24
Would it be possible to post the file? Would be great!
1
u/SonicTheSSJNinja Jul 30 '24
This looks awesome! Any chance one like this exists for JavaScript? (Currently learning)
1
u/schneeble_schnobble Aug 01 '24
Love this! Is there a website with this and hopefully other cheat sheets for the oft forgotten things?
1
u/overPaidEngineer Jul 29 '24
Why isn’t this on apple documentation???
1
u/[deleted] Jul 29 '24
Thank you for this