-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
325 lines (295 loc) · 8.44 KB
/
app.ts
File metadata and controls
325 lines (295 loc) · 8.44 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
export class Optic<S, A> {
constructor(public get: Fn<[S], A>, public put: Fn<[S, A], S>) {}
to<B>(get: Fn<[A], B>, put: Fn<[A, B], A>): Optic<S, B>
to<B>(optical: Optical<A, B>): Optic<S, B>
to<B>(...args: [Fn<[A], B>, Fn<[A, B], A>] | [Optical<A, B>]): Optic<S, B> {
const other = args.length === 1 ? optic(...args) : optic(...args)
return optic(
x => other.get(this.get(x)),
(x, y) => this.put(x, other.put(this.get(x), y)),
)
}
of<T>(get: Fn<[T], S>, put: Fn<[T, S], T>): Optic<T, A>
of<T>(optical: Optical<T, S>): Optic<T, A>
of<T>(...args: [Fn<[T], S>, Fn<[T, S], T>] | [Optical<T, S>]): Optic<T, A> {
const other = args.length === 1 ? optic(...args) : optic(...args)
return other.to(this)
}
match<B>(
matches: { [K in keyof Choice<A>]: Optical<Choice<A>[K], B> },
): Optic<S, B> {
const self = this as unknown as Optic<S, Choice<A>>
return self.to(
x => {
const key = Object.keys(x)[0]! as keyof Choice<A>
const other = optic(matches[key] as Optical<unknown, B>)
return other.get(x[key])
},
(x, y) => {
const key = Object.keys(x)[0]! as keyof Choice<A>
const other = optic(matches[key] as Optical<unknown, B>)
return { [key]: other.put(x[key], y) } as Choice<A>
},
)
}
matchTo<B>(
get: Fn<[A], B>,
matches: { [K in keyof Choice<B>]: Optical<A, A> },
): Optic<S, B> {
return {} as any
}
pick<KS extends (keyof A)[]>(...keys: KS): Optic<S, Pick<A, KS[number]>> {
return this.to(
x => {
const entries = Object.entries(x) as [keyof A, unknown][]
const filtered = entries.filter(([k, _]) => keys.includes(k))
return Object.fromEntries(filtered) as Pick<A, KS[number]>
},
(x, y) => ({ ...x, ...y }),
)
}
omit<KS extends (keyof A)[]>(...keys: KS): Optic<S, Omit<A, KS[number]>> {
return this.to(
x => {
const entries = Object.entries(x) as [keyof A, unknown][]
const filtered = entries.filter(([k, _]) => !keys.includes(k))
return Object.fromEntries(filtered) as Omit<A, KS[number]>
},
(x, y) => ({ ...x, ...y }),
)
}
map<B>(optical: Optical<ValueType<A>, B>): Optical<S, B[]> {
const other = optic(optical)
const self = this as unknown as Optic<S, ValueType<A>[]>
return self.to(
x => x.map(other.get),
(x, y) => x.map((xi, i) => other.put(xi, y[i]!)),
)
}
at: { [K in keyof A]: Optic<S, A[K]> } = {} as any
enum: { [K in keyof Choice<S>]: Optic<Choice<S>[K], A> } = {} as any
choice: { [K in keyof Choice<A>]: Optic<S, Maybe<Choice<A>[K]>> } = {} as any
view<B>(f: Fn<[A], B>): Optic<S, B> {
return this.to(
x => f(x),
x => x,
)
}
infer<T>(): S extends undefined ? Optic<T, A> : Optic<T, never> {
return this as any
}
default(data: S): Optic<Partial<S> | undefined, A> {
return this.of(
x => ({ ...data, ...x }),
(x, y) => ({ ...y, ...x }),
)
}
set(data: A): Optic<S, boolean> {
return this.update(() => data)
}
update(fn: Fn<[A], A>): Optic<S, boolean> {
return this.to<boolean>(
() => false,
(x, p) => (p ? fn(x) : x),
)
}
}
export type Optical<S, A> =
| Optic<S, A>
| ((attrs: { [K in keyof S]: Optic<S, S[K]> }) => Optical<S, A>)
| NonEmpty<{ [K in keyof A]: Optical<S, A[K]> }>
| Exclude<A, object | Function | Optic<any, any>>
export type Record<T> = keyof T extends never
? never
: Exclude<Extract<T, object>, Function>
export type Choice<T> = Intersect<T extends any ? Single<T> : never>
type Single<T> = ChoiceType<T> extends never
? Intersect<keyof T> extends never
? never
: T
: {} extends T
? never
: { [K in ChoiceType<T>]: undefined }
type ChoiceType<T> = string extends T
? never
: T extends true
? 'true'
: T extends false
? 'false'
: Extract<T, number | symbol | keyof any>
export type NonEmpty<T> = T extends any ? ({} extends T ? never : T) : never
export type Fn<Args extends unknown[], Result> = (...args: Args) => Result
export type Maybe<T> = 'Nothing' | { Just: T }
export type Intersect<T> = (T extends any ? Fn<[T], void> : never) extends Fn<
[infer I],
void
>
? [unknown] extends [I]
? never
: I
: never
export type ValueOf<T> = T[keyof T]
export type ValueType<T> = [T] extends [(infer U)[]]
? U
: [T] extends [{ [_ in any]: infer U }]
? U
: never
export type Diff<T, U> = IfEq<keyof T, keyof U, {}, Omit<T, keyof U>>
export type Common<T, U> = Pick<T, Extract<keyof T, keyof U>>
type IfEq<T, U, A, B> = [T] extends [U] ? ([U] extends [T] ? A : B) : B
export type VNode =
| { Stack: VNode[] }
| { Row: VNode[] }
| { Text: string }
| { Input: string }
| { NumberInput: number }
| { Checkbox: { label: string; checked: boolean } }
| { Button: { label: string; clicked: boolean } }
| 'Break'
| 'Divider'
| 'Empty'
export function optic<S>(): Optic<S, S>
export function optic<S, A>(get: Fn<[S], A>, put: Fn<[S, A], S>): Optic<S, A>
export function optic<S, A>(optical: Optical<S, A>): Optic<S, A>
export function optic<S, A>(
...args: [] | [Fn<[S], A>, Fn<[S, A], S>] | [Optical<S, A>]
): Optic<S, A> {
if (args.length === 0) {
return identity as Optic<S, A>
} else if (args.length === 2) {
const [get, put] = args
return new Optic(get, put)
} else {
const [optical] = args
if (optical instanceof Optic) {
return optical
} else if (optical instanceof Function) {
const { at } = optic<S>()
return optic(optical(at))
} else if (typeof optical === 'object' && optical !== null) {
return {} as any
} else {
return optic<S, A>(
_ => optical,
x => x,
)
}
}
}
export const identity = new Optic(
x => x,
(_, x) => x,
)
export function catMaybes<T>(xs: Maybe<T>[]): T[] {
return xs.reduce<T[]>(
(acc, x) => (x === 'Nothing' ? acc : [...acc, x.Just]),
[],
)
}
export function mapMaybe<T, U>(fn: Fn<[T], Maybe<U>>, xs: T[]): U[] {
return catMaybes(xs.map(fn))
}
export function on<S>(f: (x: S) => S): Optic<S, boolean> {
return optic<S>().to<boolean>(
() => false,
(s, b) => (b ? f(s) : s),
)
}
export const {
Stack,
Row,
Text,
Input,
NumberInput,
Checkbox,
Button,
Break,
Divider,
Empty,
} = optic<VNode>().enum
type App = {
newText: string
todos: Todo[]
filter: 'all' | 'active' | 'completed'
}
type Todo = {
text: string
done: boolean
}
const App = optic<App>()
export const AppView = App.default({
newText: '',
todos: [],
filter: 'all',
}).to(({ newText, todos, filter }) =>
Stack.of([
Row.of([
Input.of(newText),
Button.of({
label: 'Add Todo',
clicked: App.pick('newText', 'todos').update(({ newText, todos }) => ({
newText: '',
todos: [...todos, { text: newText, done: false }],
})),
}),
]),
Divider.infer(),
Stack.of(
todos.map(({ text, done }) =>
Row.of([
Input.of(text),
Checkbox.of({ label: 'Done', checked: done }),
Button.of({ label: 'Remove', clicked: false }),
]),
),
),
Text.of(todos.view(x => `${x.length} left`)),
Divider.infer(),
Row.of([
Button.of({ label: 'All', clicked: filter.set('all') }),
Button.of({ label: 'Active', clicked: filter.set('active') }),
Button.of({ label: 'Completed', clicked: filter.set('completed') }),
]),
]),
)
const TodosView = App.at.todos.to<VNode>(
x => ({
Row: x.map(t => ({
Button: { label: `Remove ${t.text}`, clicked: false },
})),
}),
(x, y) => {
const row = y as { Row: { Button: Choice<VNode>['Button'] }[] }
const stuff = row.Row.map<Maybe<Todo>>((n, i) =>
n.Button.clicked ? 'Nothing' : { Just: x[i]! },
)
return catMaybes(stuff)
},
)
type Counter = 'increment' | 'decrement'
const Counter = optic<number>().matchTo<Counter>(() => 'increment', {
increment: x => x + 1,
decrement: x => x - 1,
})
Counter.put(123, 'increment')
type Student = {
name: string
teacher: Teacher
}
type Teacher = {
name: string
students: Student[]
}
type User = { Student: Student } | { Teacher: Teacher }
export const UserView = optic<User>().match({
Student: ({ name, teacher }) =>
Row.of([
Text.of(name.view(x => `name: ${x}`)),
Text.of(teacher.view(x => `teacher: ${x.name}`)),
]),
Teacher: ({ name, students }) =>
Row.of([
Text.of(name.view(x => `name: ${x}`)),
Text.of(students.view(x => `students: ${x.map(s => s.name)}`)),
]),
})