-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contents.swift
298 lines (249 loc) · 7.19 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/// "over" is now "modify"
precedencegroup LeftCompositionPrecedence {
associativity: left
}
infix operator .. : LeftCompositionPrecedence
struct Lens<Whole,Part> {
let get: (Whole) -> Part
let set: (Part) -> (Whole) -> Whole
}
extension Lens {
func modify(_ transform: @escaping (Part) -> Part) -> (Whole) -> Whole {
return { whole in self.set(transform(self.get(whole)))(whole) }
}
func compose<Subpart>(_ other: Lens<Part,Subpart>) -> Lens<Whole,Subpart> {
return Lens<Whole,Subpart>(
get: { other.get(self.get($0)) },
set: { (subpart: Subpart) in
{ (whole: Whole) -> Whole in
self.set(other.set(subpart)(self.get(whole)))(whole)
}
})
}
static func .. <Subpart> (lhs: Lens<Whole,Part>, rhs: Lens<Part,Subpart>) -> Lens<Whole,Subpart> {
return lhs.compose(rhs)
}
static func zip<Part1,Part2>(
_ a: Lens<Whole,Part1>,
_ b: Lens<Whole,Part2>)
-> Lens<Whole,(Part1,Part2)>
where Part == (Part1,Part2)
{
return Lens<Whole,(Part1,Part2)>(
get: { (a.get($0),b.get($0)) },
set: { parts in { whole in b.set(parts.1)(a.set(parts.0)(whole)) } })
}
static func zip<A,B,C>(_ a: Lens<Whole,A>, _ b: Lens<Whole,B>, _ c: Lens<Whole,C>) -> Lens<Whole,(A,B,C)> where Part == (A,B,C) {
return Lens<Whole,(A,B,C)>(
get: { (a.get($0),b.get($0),c.get($0)) },
set: { parts in { whole in c.set(parts.2)(b.set(parts.1)(a.set(parts.0)(whole))) } })
}
}
struct Prism<Whole,Part> {
let tryGet: (Whole) -> Part?
let inject: (Part) -> Whole
}
enum Either<A,B> {
case left(A)
case right(B)
}
extension Prism {
func tryModify(_ transform: @escaping (Part) -> Part) -> (Whole) -> Whole {
return { whole in self.tryGet(whole).map { self.inject(transform($0)) } ?? whole }
}
func compose<Subpart>(_ other: Prism<Part,Subpart>) -> Prism<Whole,Subpart> {
return Prism<Whole,Subpart>(
tryGet: { self.tryGet($0).flatMap(other.tryGet) },
inject: { self.inject(other.inject($0)) })
}
static func .. <Subpart> (lhs: Prism<Whole,Part>, rhs: Prism<Part,Subpart>) -> Prism<Whole,Subpart> {
return lhs.compose(rhs)
}
static func zip<Part1,Part2>(
_ a: Prism<Whole,Part1>,
_ b: Prism<Whole,Part2>)
-> Prism<Whole,Either<Part1,Part2>>
where Part == Either<Part1,Part2>
{
return Prism<Whole,Either<Part1,Part2>>(
tryGet: { a.tryGet($0).map(Either.left) ?? b.tryGet($0).map(Either.right) },
inject: { part in
switch part {
case .left(let value):
return a.inject(value)
case .right(let value):
return b.inject(value)
}
})
}
}
enum ViewState<T> {
case empty
case processing(String)
case failed(Error)
case completed(T)
}
struct LoginPage {
var title: String
var credentials: CredentialBox
var buttonState: ViewState<Button>
}
struct CredentialBox {
var usernameField: TextField
var passwordField: TextField
}
struct TextField {
var text: String
var placeholder: String?
var secureText: Bool
}
struct Button {
var title: String
var enabled: Bool
}
extension CredentialBox {
enum lens {
static let usernameField = Lens<CredentialBox,TextField>.init(
get: { $0.usernameField },
set: { part in
{ whole in
var m = whole
m.usernameField = part
return m
}
})
}
}
extension ViewState {
enum prism {
static var processing: Prism<ViewState,String> {
return .init(
tryGet: {
guard case .processing(let message) = $0 else {
return nil
}
return message
},
inject: { .processing($0) })
}
}
}
let savedUsername = "foobar"
let oldModel = LoginPage.init(title: "", credentials: CredentialBox.init(usernameField: TextField.init(text: "", placeholder: nil, secureText: false), passwordField: TextField.init(text: "", placeholder: nil, secureText: true)), buttonState: .completed(Button.init(title: "", enabled: false)))
let initialState = (
title: "Welcome back!",
username: savedUsername,
buttonState: ViewState<Button>.completed(Button.init(
title: "Login",
enabled: false)))
var m_newModel = oldModel
m_newModel.title = initialState.title
m_newModel.credentials.usernameField.text = initialState.username
m_newModel.buttonState = initialState.buttonState
extension LoginPage {
enum lens {
static let title = Lens<LoginPage,String>.init(
get: { $0.title },
set: { part in { var m = $0; m.title = part; return m }})
static let credentials = Lens<LoginPage,CredentialBox>.init(
get: { $0.credentials },
set: { part in { var m = $0; m.credentials = part; return m }})
static let buttonState = Lens<LoginPage,ViewState<Button>>.init(
get: { $0.buttonState },
set: { part in { var m = $0; m.buttonState = part; return m }})
}
}
extension TextField {
enum lens {
static let text = Lens<TextField,String>.init(
get: { $0.text },
set: { part in { var m = $0; m.text = part; return m }})
}
}
let titleLens = LoginPage.lens.title
let usernameTextLens = LoginPage.lens.credentials..CredentialBox.lens.usernameField..TextField.lens.text
let buttonStateLens = LoginPage.lens.buttonState
let newModel1 = titleLens.set(initialState.title)(usernameTextLens.set(initialState.username)(buttonStateLens.set(initialState.buttonState)(oldModel)))
let initialStateLens = Lens.zip(
titleLens,
usernameTextLens,
buttonStateLens)
let newModel2 = initialStateLens
.set(initialState)(oldModel)
func advanceProcessingMessage(_ previous: String) -> String {
switch previous {
case "":
return "Please wait"
case "Please wait":
return "Almost there"
case "Almost there":
return "ALMOST THERE"
default:
return previous + "!"
}
}
let processingPrism = ViewState<Button>.prism.processing
let newModel3 = buttonStateLens.modify(processingPrism.tryModify(advanceProcessingMessage))(oldModel)
/// ((ViewState<Button>) -> ViewState<Button>) -> (LoginPage) -> LoginPage
let modifyLoginPage = buttonStateLens.modify
/// ((String) -> String) -> (ViewState<Button>) -> ViewState<Button>
let modifyProcessingMessage = processingPrism.tryModify
infix operator >>>
func >>> <A,B,C> (
_ left: @escaping (A) -> B,
_ right: @escaping (B) -> C)
-> (A) -> C
{
return { right(left($0)) }
}
let onProcessing = modifyProcessingMessage >>> modifyLoginPage
let newModel4 = onProcessing(advanceProcessingMessage)(oldModel)
struct LensLaw {
static func setGet<Whole, Part>(
_ lens: Lens<Whole,Part>,
_ whole: Whole,
_ part: Part)
-> Bool where Part: Equatable
{
return lens.get(lens.set(part)(whole)) == part
}
}
extension Dictionary {
static func lens(at key: Key) -> Lens<Dictionary,Value?> {
return Lens<Dictionary,Value?>(
get: { $0[key] },
set: { part in
{ whole in
var m_dict = whole
m_dict[key] = part
return m_dict
}
})
}
}
extension WritableKeyPath {
var lens: Lens<Root,Value> {
return Lens<Root,Value>.init(
get: { whole in whole[keyPath: self] },
set: { part in
{ whole in
var m = whole
m[keyPath: self] = part
return m
}
})
}
}
let passwordLens = (\LoginPage.credentials.passwordField.text).lens
extension Optional {
static var prism: Prism<Optional,Wrapped> {
return Prism<Optional,Wrapped>.init(
tryGet: { $0 },
inject: Optional.some)
}
}
struct Affine<Whole,Part> {
let tryGet: (Whole) -> Part?
let trySet: (Part) -> (Whole) -> Whole?
}
"OK"