forked from pointfreeco/episode-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
162 lines (120 loc) · 2.97 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
struct Pair<A, B> {
let first: A
let second: B
}
Pair<Void, Void>.init(first: (), second: ())
Pair<Bool, Void>.init(first: true, second: ())
Pair<Bool, Void>.init(first: false, second: ())
Pair<Bool, Bool>.init(first: true, second: true)
Pair<Bool, Bool>.init(first: true, second: false)
Pair<Bool, Bool>.init(first: false, second: true)
Pair<Bool, Bool>.init(first: false, second: false)
enum Three { case one, two, three }
Pair<Bool, Three>.init(first: true, second: .one)
Pair<Bool, Three>.init(first: false, second: .one)
Pair<Bool, Three>.init(first: true, second: .two)
Pair<Bool, Three>.init(first: false, second: .two)
Pair<Bool, Three>.init(first: true, second: .three)
Pair<Bool, Three>.init(first: false, second: .three)
//Pair<Bool, Never>.init(first: true, second: <#T##Never#>)
enum Either<A, B> {
case left(A)
case right(B)
}
Either<Void, Void>.left(())
Either<Void, Void>.right(())
Either<Bool, Void>.left(true)
Either<Bool, Void>.left(false)
Either<Bool, Void>.right(())
Either<Bool, Bool>.left(true)
Either<Bool, Bool>.left(false)
Either<Bool, Bool>.right(true)
Either<Bool, Bool>.right(false)
Either<Bool, Three>.left(true)
Either<Bool, Three>.left(false)
Either<Bool, Three>.right(.one)
Either<Bool, Three>.right(.two)
Either<Bool, Three>.right(.three)
Either<Bool, Never>.left(true)
Either<Bool, Never>.left(false)
//Either<Bool, Never>.right(???)
struct User {
let id: Int
let isAdmin: Bool
let name: String
}
User.init
[1, 2, 3, 4].map(String.init)
["1", "2", "blob", "3"].compactMap(Int.init)
Either<Int, String>.left
Either<Int, String>.right
[1, 2, 3, 4].map(Either<Int, String>.left)
let value = Either<Int, String>.left(42)
switch value {
case let .left(left):
print(left)
case let .right(right):
print(right)
}
func compute(_ xs: [Int]) -> (product: Int, sum: Int, average: Double) {
var product = 1
var sum = 0
xs.forEach { x in
product *= x
sum += x
}
return (product, sum, Double(sum) / Double(xs.count))
}
let result = compute([1, 2, 3, 4, 5])
result.product
result.sum
let (product, sum, average) = compute([1, 2, 3, 4, 5])
product
sum
average
let tuple: (id: Int, name: String) = (42, "Blob")
[1, 2, 3].map { $0 + 1 }
tuple.0
tuple.1
tuple.id
tuple.name
[1, 2, 3, 4, 5].reduce((product: 1, sum: 0)) { accum, x in
(accum.product * x, accum.sum + x)
}
3 * 4
//3 + 4
//3 `addThreeToFour` 4
let _: (Int, String)
let choice: (Int | String) = .0(42)
let choice: (Int | String) = .1("Blob")
let choice: (id: Int | param: String) = .id(42)
let choice: (id: Int | param: String) = .param("Blob")
switch choice {
case let .0(id):
print(id)
case let .1(param):
print(param)
}
switch choice {
case let .id(id):
print(id)
case let .param(param):
print(param)
}
//enum Optional<A> {
// case some(A)
// case none
//}
enum Loading<A> {
case some(A)
case loading
}
enum EmptyCase<A> {
case some(A)
case emptyState
}
//Loading<EmptyCase<[User]>>
func render(data: (user: [User] | empty | loading | )) {
switch data {
}
}