Group Group Group Group Group Group Group Group Group

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!