Skip to content

Commit f787e49

Browse files
author
Bob Lee
committed
Finished Reduce
1 parent 013d92e commit f787e49

File tree

4 files changed

+96
-16
lines changed

4 files changed

+96
-16
lines changed

course/functional-programming/reduce.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Reduce
22
## Introduction
3+
Welcome to Lesson 3 of Functional Programming. I don't think I have to talk too much. Let's get into the lesson right away.
34

45
## Problem
56
Combine all into one
@@ -34,13 +35,90 @@ let added = Array(1...10).reduce(0) { $0 + $1 } // 55
3435
let subtracted = Array(1...10).reduce(0) { $0 - $1 } // -55
3536
```
3637

37-
You will discover how it works behind the scene by the end of this lesson. Let's get started.
38+
You will discover how the `reduce` function works behind the scene by the end of this lesson. Let's get started.
3839

40+
```swift
41+
func myReduce(_ seed: Int, numbers: [Int], operation: (Int, Int) -> Int) {
42+
var current = seed
43+
for number in numbers {
44+
current = operation(current, number)
45+
}
46+
}
47+
```
3948

49+
`seed` represents the initial value you wish to start off with.
4050

41-
### Source Code
51+
```swift
52+
myReduce(0, numbers: Array(1...10)) { $0 + $1 }
53+
```
54+
55+
### Ex) Finding the Max
56+
```swift
57+
let maxNumber = Array(1...10).reduce(0) { (total, number) in max(total, number) }
58+
let bigNumber = Array(1...10).reduce(0) { max($0, $1) }
59+
```
60+
61+
So far, `myReduce` has not returned any. Let's create a function that returns the final value in `Int`.
62+
63+
```swift
64+
func reduce(_ seed: Int, numbers: [Int], operation: (Int, Int) -> Int) -> Int {
65+
var current = seed
66+
for number in numbers {
67+
current = operation(current, number)
68+
}
69+
return current
70+
}
71+
72+
reduce(0, numbers: Array(1...10)) { $0 + $1 }
73+
```
74+
75+
### Generic Reduce
76+
Again, creating a generic function is just a required for the sake of time and efficiency.
4277

43-
### Resources
78+
```swift
79+
extension Array {
80+
func myReduce<U>(_ seed: U, operation:(U, U) -> U) -> U {
81+
var current = seed
82+
for item in self {
83+
current = operation(current, item as! U)
84+
}
85+
return current
86+
}
87+
}
88+
```
89+
90+
Currently, there is a force casting using `as! U`. It occurs since the type of `U` is not defined until you use the `myReduce` function.
91+
92+
```swift
93+
let names = ["Bob", "Bobby", "Lee"]
94+
let description = names.myReduce("Names:") { "\($0) " + $1 }
95+
print(description) // "Names: Bob Bobby Lee"
96+
```
97+
98+
Not only that, you may "chain" multiple functions.
4499

100+
```swift
101+
let lowerNames = names.map { $0.lowercased() }.myReduce("Names:") { "\($0) " + $1 }
102+
print(lowerNames) // "Names: bob bobby lee"
103+
```
104+
105+
### The Purest Form
106+
```swift
107+
//: The Purest Form
108+
extension Array {
109+
func reduce(_ seed: Element, operation:(Element, Element) -> Element) -> Element {
110+
var current = seed
111+
for item in self {
112+
current = operation(current, item)
113+
}
114+
return current
115+
}
116+
}
117+
118+
let hello = ["Bob", "Bobby", "Lee"].reduce("Names:") { "\($0) " + $1 }
119+
```
120+
### Source Code
121+
[6004_reduce.playground]()
45122

46123
## Conclusion
124+
I hope by now you are truly seeing the power of functions. Well, a functional paradigm is not specifically tied to iOS development. In fact, you've learned how to apply and utilize closure and generics in web development, server development, Android development, and other platforms. This course isn't just about learning Swift. It is about to learn how to code with beauty and class. Let's get to the final function of this chapter.
2 KB
Binary file not shown.

source-code/6000_functional_programming/6004_reduce.playground/Contents.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*:
22
## Learn Swift with Bob
33
### Functional Programming
4-
### Reduce
4+
### 6004_Reduce
55

66
**Problem:** Combine all into one
77

@@ -30,32 +30,32 @@ let subtracted = Array(1...10).reduce(0) { $0 - $1 } // -55
3030

3131

3232
//: Let's Find Out
33-
func myReduce(seed: Int, numbers: [Int], operation: (Int, Int) -> Int) {
33+
func myReduce(_ seed: Int, numbers: [Int], operation: (Int, Int) -> Int) {
3434
var current = seed
3535
for number in numbers {
3636
current = operation(current, number)
3737
}
3838
}
3939

40-
myReduce(seed: 0, numbers: Array(1...10)) { $0 + $1 }
40+
myReduce(0, numbers: Array(1...10)) { $0 + $1 }
4141

4242
//: Max Number
43-
let maxNumber = Array(1...10).reduce(0) { (total, number) in max(total, number) }
44-
let bigNumber = Array(1...10).reduce(0) { max($0, $1) }
43+
let maxNumber = Array(1...10).myReduce(0) { (total, number) in max(total, number) }
44+
let bigNumber = Array(1...10).myReduce(0) { max($0, $1) }
4545

4646
//: Create Function that Returns
47-
func reduce(seed: Int, numbers: [Int], operation: (Int, Int) -> Int) -> Int {
47+
func reduce(_ seed: Int, numbers: [Int], operation: (Int, Int) -> Int) -> Int {
4848
var current = seed
4949
for number in numbers {
5050
current = operation(current, number)
5151
}
5252
return current
5353
}
5454

55-
reduce(seed: 0, numbers: Array(1...10)) { $0 + $1 }
55+
reduce(0, numbers: Array(1...10)) { $0 + $1 }
5656
//: Generic Reduce
5757
extension Array {
58-
func myReduce<U>(seed: U, operation:(U, U) -> U) -> U {
58+
func myReduce<U>(_ seed: U, operation:(U, U) -> U) -> U {
5959
var current = seed
6060
for item in self {
6161
current = operation(current, item as! U)
@@ -64,16 +64,18 @@ extension Array {
6464
}
6565
}
6666

67+
//: Example
6768
let names = ["Bob", "Bobby", "Lee"]
68-
let description = names.myReduce(seed: "Names:") { "\($0) " + $1 }
69+
let description = names.myReduce("Names:") { "\($0) " + $1 }
6970
print(description)
7071

71-
//: Example
72-
let lowerNames = names.map { $0.lowercased() }.myReduce(seed: "Names:") { "\($0) " + $1 }
72+
//: Multiple Functions
73+
let lowerNames = names.map { $0.lowercased() }.myReduce("Names:") { "\($0) " + $1 }
7374
print(lowerNames)
75+
7476
//: The Purest Form
7577
extension Array {
76-
func reduce(seed: Element, operation:(Element, Element) -> Element) -> Element {
78+
func reduce(_ seed: Element, operation:(Element, Element) -> Element) -> Element {
7779
var current = seed
7880
for item in self {
7981
current = operation(current, item)
@@ -82,7 +84,7 @@ extension Array {
8284
}
8385
}
8486

85-
let hello = ["Bob", "Bobby", "Lee"].reduce(seed: "Names:") { "\($0) " + $1 }
87+
let hello = ["Bob", "Bobby", "Lee"].reduce("Names:") { "\($0) " + $1 }
8688

8789

8890

-10 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)