Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

19. Conclusion

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 18. Challenge: filter & reduce Next episode: Part 2 Quiz: Closures

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 19. Conclusion

More About Algorithms

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Transcript: 19. Conclusion

Wow! That was a lot of new information! Nice job working through that. Let’s do a quick recap of all of those collection methods that take a closure parameter!

forEach is very similar to a for loop. The key differences are that you can’t use things like break or continue to control execution flow with forEach. You also can’t pre-filter content with a where clause.

What you can do with forEach that you can’t with a for loop is easily chain it with other methods, like filter! You can also pass functions into forEach, so, if you just want to apply some existing function to each element, that can be handy.

map works nearly the same as forEach, but it returns an array. It’s great for transforming a collection of one type into another type, or apply a some change to a collection of data.

The job of map’s closure is to perform whatever kind of transformation you want for each element, and return the result.

compactMap helps you weed out those unwanted nil values from collections of optionals or ensure you only get the successful results from performing operations that might return nil.

The job of compactMap’s closure is the same as in map! compactMap does all that magical nil filtering for you behind the scenes.

flatMap handles multi-dimensional arrays! If you want to do some work that results in combining multiple arrays into one, flatMap is the method for you!

flatMap’s closure argument expects an array as input, so its job is a little different than in map and compactMap, which expect a single element. This closure deals with each individual array, and returns an array of whatever type you want the final result to be.

filter can be an excellent alternative to where clauses and if conditions, especially when you’re trying to chain multiple methods together. It’s also useful when your filtering criteria get more complex!

The job of filter’s closure is to say “Yes, this belongs in the returned collection” or “Nope, toss it out!”. filter handles the creation of the returned collection for you!

reduce and reduceInto are a good candidates when you might have used a for loop along with some kind of empty starting value, like zero, or an empty array or dictionary.

Use sort or sorted when you want to sort things and you just want it done the “regular” way - numerically going up or alphabetically from A to Z. But sort(by) and sorted(by) are also there for you if you want to specify your own sorting criteria without actually writing your own sorting algorithm.

Check out the Swift Algorithm Club repo, our Data Structures & Algorithms book, and the course for much deeper explanations of sorting algorithms, and how to write them yourself!

There are a couple more of these kinds of methods that I didn’t cover, but now, with your experience, you should be able to try them out on your own.

So, take a look at mapValues and compactMapValues! Those are methods similar to map and compactMap that specifically work on dictionary values and return dictionaries.

What I haven’t said yet, but you may already know, is that all of the methods I’ve been talking about are usually associated with the term “Functional Programming”.

That’s a deep topic all on its own, but, briefly, functional programming is a style of programming that prioritizes immutability and makes frequent use of higher-order functions. So, congratulations! You learned some functional programming techniques.

Even if you never use any of those methods again, you’ll find it useful to have a basic understanding of closures.

Whether you’re developing with SwiftUI or UIKit, you’ll encounter tons of methods with closure parameters. And wouldn’t it be nice to be able to confidently write those inline, instead of writing functions you’d only use once?

Coming up next, we’ll move away from the compound types we’ve been working with, and focus on named types, starting with the Enumeration!