From the course: Learning Functional Programming with Swift
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Filtering - Swift Tutorial
From the course: Learning Functional Programming with Swift
Filtering
- [Instructor] The next important built-in array function is called filter. Filter is used when you want to extract all of the elements in an array that fit some kind of criteria. For example, if we have an array of numbers and we want to get all the numbers from it that are even. Or if we have an array of employee data and we want to find the employees from this array that make more than a certain amount per year. These are things that filter can help us with. The syntax of filter is similar to map. We can call it on any array and then we pass in a function. The main difference between filter and map is the type of function that we pass into it. In map, we pass in a function that returns a value for each element in an array. The return value of this function represents what an element becomes in our new array. On the other hand for filter, we pass in a function that returns either true or false for each element. If the function that we pass returns true for a given element, then the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.