We show some examples of how Swift's built-in filter function can be used to improve our code and work with arrays.
- [Instructor] So again, let's say that we have…our simple array of numbers.…Let numbers equal one, two, three, four, five,…six, seven, eight, nine.…Now, what if we want to get only the even numbers…from our array?…The procedural way to do this…would be to create a new array.…Var evenNumbers equals an empty array.…And then we create a for loop to loop through each number.…For number in numbers.…
And what we want to do is check if each number is even,…and if it is, then append it to the even numbers array.…The way we check if a number is even…is if the number modulus two is zero.…Basically, this means that if we divide the number by two,…there's no remainder.…It's a simple way to check…and see if a number is even in code.…So that will look like…number modulus, which is the percent sign,…two is equal to zero.…And if it is, then we want to append this number…to our even numbers array.…
evenNumbers dot append number.…So now let's comment out this code…and look at a functional way to do it using filter.…The first thing we do is take this condition…
- What is functional programming?
- Keeping functions and data separate
- First-class functions
- Working with arrays functionally
- Filtering and reducing
- Partial-application and recursion
Skill Level Intermediate
Swift 4: Protocol-Oriented Programmingwith Károly Nyisztor1h 44m Intermediate
Swift: Delegations and Data Sourceswith Steven Lipton1h 6m Intermediate
Swift: Writing Reusable Frameworkswith David Okun1h 49m Intermediate
1. What Is Functional Programming?
2. First-Class Functions
3. Array Functions
4. Advanced Functional Concepts
Next steps1m 1s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.