From the course: Learning Higher-Order Functions with Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Filter

Filter

From the course: Learning Higher-Order Functions with Swift

Start my 1-month free trial

Filter

- [Instructor] So the next higher order function we're going to look at is filter. So filter does what it says. It filters. It filters out items from a collection. So once again, just like the other ones we've looked at, it takes a closure with one parameter. Just like we saw before with map, and the parameter type is the type of the items in our array. And it returns a bool. If your closure returns true, it keeps it. If it returns false, it filters it out. So the result of filter is going to be an array of the same type as your collection. So strings, ints, whatever the original one, as the same size or smaller. If you don't filter out any, it's just going to be another array of the same things. So let's take a look at that in our playground. Okay so we have this array of names, and we want to filter out any names with a length less than five. So we know we're going to filter out these first two. I'll put another one at the end that's short. I'll just say it's ASD, so that we know…

Contents