Skip to content

Commit 0e38e5f

Browse files
author
Bob Lee
committed
Semi-prepped for Flatmap
1 parent 07b2282 commit 0e38e5f

File tree

9 files changed

+56
-95
lines changed

9 files changed

+56
-95
lines changed

source-code/.DS_Store

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

source-code/10000_advanced-swift/10006_associated_type_constraints.playground/Pages/8001_nested_generics_recursive_enum.xcplaygroundpage/Contents.swift

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,87 +8,31 @@
88
---
99
*/
1010

11-
//: Test Driven Development
12-
// -Onone // none a.k.a. debug
13-
// -O // fast a.k.a. release
14-
// -Ounchecked // super fast testing
11+
1512

16-
/*:
13+
// Element Associated Type
14+
let twoDArray = [1, 2, 3]
15+
let threeDArray = [[1], [2], [3]]
1716

18-
Standard Swift library come with five assertion functions
19-
1. assert()
20-
2. assertionFailure()
21-
3. precondition()
22-
4. preconditionFailure()
23-
5. fatalError()
17+
extension Array {
18+
func typeCheckWithInt() {
19+
if type(of: self) == [Int].self {
20+
print("2D array")
21+
}
22+
if type(of: self) == [[Int]].self {
23+
print("3D array")
24+
}
25+
}
2426

25-
*/
26-
//: assert()
27-
assert(true)
28-
29-
30-
func enterName(name: String) {
31-
if name == "" {
32-
assert(false, "You must enter a full name")
33-
} else if name == "Bob" {
34-
assert(false, "There is only one Bob")
35-
}
36-
}
37-
38-
// enterName(name: "Bob")
39-
40-
print("File: \(#file)")
41-
print("Line: \(#line)")
42-
43-
44-
45-
var expectedResult = 10
46-
var actualResult = 10
47-
48-
assert(actualResult == expectedResult, "The actual result doesn't match with the expected")
49-
50-
51-
//: assertionFailure()
52-
53-
import Foundation
54-
let randomNumber: Int = Int(arc4random_uniform(3))
55-
56-
57-
switch randomNumber {
58-
case 0, 1, 2:
59-
print(randomNumber)
60-
default:
61-
assertionFailure("Unexpected index \(randomNumber)")
62-
}
63-
64-
//: precondition()
65-
// Identical as assert()
66-
let expectedNumber = (1, 3)
67-
let actualNumber = (1, 3)
68-
69-
precondition(actualNumber == expectedNumber, "\(actualNumber) is not the same as \(expectedNumber)")
70-
71-
//: PreconditionFailture()
72-
// preconditionFailure("Good")
73-
74-
75-
//: fatalError()
76-
let number: Int = Int(arc4random_uniform(100))
77-
78-
func enterNumberReturnString(index: Int) -> String {
79-
switch index {
80-
case 0, 1, 2:
81-
return "\(number)"
82-
default:
83-
// assertionFailure("Unexpected index \(number)")
84-
// abort()
85-
fatalError("Unepxpected index \(number)")
86-
}
87-
}
88-
89-
//: > `@noreturn`, the compiler confirms that the marked function will not return. The application would terminate instead.
90-
91-
27+
func typeCheckWithElement() {
28+
if type(of: self) == [Element].self {
29+
print("2D array")
30+
}
31+
if type(of: self) == Element.self {
32+
print("3D array")
33+
}
34+
}
35+
}
9236

9337

9438

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='6.0' target-platform='ios' display-mode='rendered'/>
2+
<playground version='6.0' target-platform='ios' display-mode='raw'/>
1.31 KB
Binary file not shown.
0 Bytes
Binary file not shown.
-7 Bytes
Binary file not shown.

source-code/6000_functional_programming/6005_flatmap.playground/Contents.swift

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,38 @@
1010
*/
1111

1212

13+
let example = [[5,2,7],[4,8],[9,1,3]]
14+
let result = example.flatMap { $0 }
15+
print(result)
16+
17+
let anotherExample: [Int?] = [1, 2, nil, 3, 4, 5, nil, nil]
18+
let AnotherRsult = anotherExample.flatMap { $0 }
19+
20+
21+
func myFlatMap(numbers: [Int]) {
22+
23+
}
24+
25+
26+
func bobFlatMap(numbers: [[Int]], operation: (Int) -> Int) {
27+
var result: [Int] = []
28+
for array in numbers {
29+
for item in array {
30+
result.append(item)
31+
}
32+
}
33+
print(result)
34+
}
35+
36+
37+
bobFlatMap(numbers: [[1, 2, 3], [4, 5], [6, 7, 8]], operation: { $0 })
38+
bobFlatMap(numbers: [[1, 2, 3, 4, 5]], operation: { $0 })
39+
40+
1341

1442

1543
extension Array {
16-
func flatMap<U>(transform: (Element) -> U?) -> [U] {
44+
func myFlatMap<U>(transform: (Element) -> U?) -> [U] {
1745
var result = [U]()
1846
for item in map(transform) {
1947
if let nonOptional = item {
@@ -24,23 +52,12 @@ extension Array {
2452
}
2553
}
2654

55+
let closure: (Int) -> Int? = { $0 }
2756

2857

29-
30-
31-
32-
33-
34-
35-
36-
let collections = [[5,2,7],[4,8],[9,1,3]]
37-
let flat = collections.flatMap { $0 }
38-
print(flat)
39-
40-
41-
let onlyEvenSimpler = collections.flatMap { $0.filter { $0 % 2 == 0 } }
42-
43-
58+
let myResult = [[1], [2], [3], nil].myFlatMap { $0 }
59+
let flattenCollection = myResult.joined()
60+
let flattenArray = Array(flattenCollection)
4461

4562

4663

3.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)