You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: course/functional-programming/filter.md
+65-19
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,8 @@ How to get things done with one line of code?
8
8
> **Note:** Most people use Swift functions without knowing the behind implementation
9
9
10
10
### Imperative/Non-functional
11
+
Let's attempt to grab even numbers using a non-functional approach.
12
+
11
13
```swift
12
14
// Get even numbers
13
15
var evens = [Int]()
@@ -19,16 +21,20 @@ for i in 1...10 {
19
21
}
20
22
```
21
23
24
+
It certainly works. But, you know what I'm about to say.
25
+
22
26
### Declarative/Functional
23
-
You may execute the task above using the built-in `filter`method by the `Array` struct. However, you will discover how things work throughout this lesson. If you are stuck, don't you worry. I will walk you through.
27
+
If you remember correct, `Array` is just a `struct` and it contains built-in methods. `filter`is one of them. Let us utilize the `filter` function to get things done. You don't have to understand what goes under the hood at this point, but you will by creating our own custom `filter` function.
24
28
25
29
```swift
26
30
Array(1...10).filter { (number) in number %2==0 }
27
31
Array(1...10).filter { $0%2==0 }
28
32
```
29
33
34
+
Let us find out what the heck is going on. Don't you worry. As long as you are comfortable with closures and generics, you will get through with me.
35
+
30
36
### Become an A Student
31
-
My mom used to like when I handed my grade sheet filled with "A"s. Let's find out how to only get "A" letters to please my mom.
37
+
My mom used to love (still do since I'm in college) when I handed my grade sheet filled with "A"s. Let's find out how to only get "A" letters to please my mom.
32
38
33
39
```swift
34
40
let recentGrade = ["A", "A", "A", "A", "B", "D"] // It can be any data
@@ -56,6 +62,7 @@ The code above tells you the steps how you filtered `recentGrade`. This is long,
56
62
From now on, you might find it difficult. Bear with me. First of all, let's create a function either returns `true` or `false` based on the parameter.
57
63
58
64
Create closure block that returns true/false
65
+
59
66
```swift
60
67
var isMomHappy: (String) ->Bool= { grade inreturn grade =="A" }
61
68
isMomHappy = { $0=="A" }
@@ -64,6 +71,8 @@ isMomHappy("A") // true
64
71
isMomHappy("B") // true
65
72
```
66
73
74
+
Create a function that take the closure block whose type is `(String) -> Bool`. I will call the parameter as `operation`.
let happyGrade =stringFilter(grades: myGrade, operation: isMomHappy)
114
+
happyGrade // ["A", ...]
115
+
```
116
+
117
+
### Pass Closure Directly
118
+
So far, you've passed the closure block indirectly to the `operation` parameter using `isMomHappy`. However, you may directly enter a closure block. The syntax may look strange but I'm sure you are comfortable with closures at this point.
If you remember from Chapter 1, intro to generics, it doesn't make sense to create a distinct function just for `String` only. Let us utilize the Swift generics.
The built-in `filter` may look something like this. Since you have not taken generics protocols in the following chapter and Advanced Swift, you may be confused with `Element`. It's the type of an array for now.
147
188
148
189
```swift
149
190
extensionArray {
@@ -159,14 +200,19 @@ extension Array {
159
200
}
160
201
```
161
202
162
-
Test it out.
203
+
Let us test it out.
163
204
164
205
```swift
165
206
let result =Array(1...100).myFilter { $0%2==0 }
166
207
```
167
208
168
209
### Source Code
210
+
[6002_filter.playground]()
169
211
170
212
### Resources
213
+
[Intro to Functional Programming](https://blog.bobthedeveloper.io/intro-to-swift-functional-programming-with-bob-9c503ca14f13)
171
214
172
215
## Conclusion
216
+
Congratulations. You've learned what goes under the hood of `filter`, one of the most common functions. From now on, you understand what it means to pass a closure block at the end and you've learned how the closure block is used within the main function. This principle applies in every other major functions we will take a look at. Along with generics, it allows reusability, thus scalability in your code base.
217
+
218
+
At the end of the lesson, I've mentioned `Element` of an array. I don't expect you to know what it is. However, in the last chapter of this course, you will learn the behind scene by understanding the statement, "Swift is a protocol oriented programming language". Let's continue.
Copy file name to clipboardExpand all lines: course/functional-programming/intro-functional-paradigm.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -97,4 +97,4 @@ A functional paradigm ensures no mutability or no change in state when used.
97
97
## Conclusion
98
98
Now, I've demystified the concept of "Functional Programming" for you. Again, based on my simplest definition, it's nothing more than using functions to solve problems.
99
99
100
-
In the Swift Foundation library built by Apple Engineers there are dozens of useful functions for us developers may use. However, as a developer, we've got to know the behind scene of each function. In the rest of the chapter, you will learn how those functions are created and you will be able to produce on your custom functions depending on your need. From now on, the lesson can get rough and tough, again, make sure you completely understand closures and generics before you jump in.
100
+
In the Swift Foundation library built by Apple Engineers, there are dozens of useful functions for us developers may use. However, as a developer, we've got to know the behind scene of each function. In the rest of the chapter, you will learn how those functions are created and you will be able to produce on your custom functions depending on your need. From now on, the lesson can get rough and tough, again, make sure you completely understand closures and generics before you jump in.
0 commit comments