Programming in Swift: Functions and Types · Challenge: Closures & Collections | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/16

Hi, I tried to follow the challenges but the files in the materials are empty

Hi! I just tried downloading the materials myself and both the “Begin” and “End” playgrounds seem correct. Let me know if this is still giving you trouble :]

Hello, curious about something I noticed in playground sometimes with # of calls made on code - and in this case specifically the compactMap challenge #2.

One variation of a solution I wrote…

var classPets2 = students.compactMap { (student) -> String? in
student.pet

}

…appears to be called 4 times (or is it more precise to say iterated?) in playground.
But a shorter trailing closure form…

var classPets3 = students.compactMap { $0.pet }

… appears to be called/iterated 5 times. But to confuse things, it appears that same code but on multiple lines iterates only 4 times…

var classPets4 = students.compactMap {

$0.pet

}

I’m curious what is going on under the hood for the differences? And does this have real world implications in speed of code then for writing longer form vs shorter trailing closure form? I know it might be beyond the scope of a reply - if there’s a specific tutorial or article here that explains it in a larger context, feel free to simply share the link! Thank you - this tutorial is great btw!

Hi Catie,
Thanks for the great videos!
Both Begin and End materials are empty for me

Hi! Thanks for your question. There shouldn’t be any real difference between the way the long or short forms of the closure compile.

What you’re seeing with the closure on one line is a sort of artifact of how playgrounds show you what’s going on. You’re seeing the 4 iterations of $0.pet that you’d expect from compactMap, plus that initial assignment to classPets, which only shows up in a count in the sidebar when you put the closure on one line :woman_shrugging:t4:.

Hi there! Glad you’re liking the videos :]

The playground files for this course have multiple pages. To see all exercises and challenges, open the Project Navigator using the “Hide or show the Navigator” button in the upper left corner of the window, or the keyboard shortcut Command-0.

projectNavigator

Thank you! It wasn’t keeping me from actually making my way through the tutorial, was just more of an “under the hood” curiosity.

“The Book of Atrus” Wow that’s a deep cut to my middle-school days.

1 Like

Hi. I have a question. So forEach, map don’t require to check if the value is nil or not like in for loop?

Hi, Xcode says flatMap has been deprecated, use compactMap instead.
Could you explain how to use compactMap to flatten a 2D array?

Hello! flatMap used to have an overload that has been deprecated in favor of compactMap for use in filtering out optionals. The warning you are seeing thinks you are trying to use the deprecated overload.

flatMap is still the correct choice for flattening arrays! Sometimes Xcode will still give me a warning until I’ve finished up with the chunk of code I’m flatMap-ing, but the warning you see should specify:

‘flatMap’ is deprecated: Please use compactMap( _: ) for the case where closure returns an optional value