-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.ts
697 lines (583 loc) · 20.7 KB
/
index.ts
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import { HKT, HKTS, HKTAs, HKT2S, HKT2As } from 'fp-ts/lib/HKT'
import { Monoid, monoidArray, monoidAll, monoidAny } from 'fp-ts/lib/Monoid'
import { Applicative } from 'fp-ts/lib/Applicative'
import { Foldable, foldMap } from 'fp-ts/lib/Foldable'
import { Traversable } from 'fp-ts/lib/Traversable'
import * as option from 'fp-ts/lib/Option'
import { Option, none, some } from 'fp-ts/lib/Option'
import { identity, constant, Predicate } from 'fp-ts/lib/function'
import * as id from 'fp-ts/lib/Identity'
import * as const_ from 'fp-ts/lib/Const'
/*
Laws:
1. get . reverseGet = identity
2. reversetGet . get = identity
*/
export class Iso<S, A> {
readonly _tag: 'Iso' = 'Iso'
constructor(readonly get: (s: S) => A, readonly reverseGet: (a: A) => S) {
this.unwrap = this.to = get
this.wrap = this.from = reverseGet
}
modify(f: (a: A) => A): (s: S) => S {
return s => this.reverseGet(f(this.get(s)))
}
/** view an Iso as a Lens */
asLens(): Lens<S, A> {
return new Lens(this.get, a => s => this.reverseGet(a))
}
/** view an Iso as a Prism */
asPrism(): Prism<S, A> {
return new Prism(s => some(this.get(s)), this.reverseGet)
}
/** view an Iso as a Optional */
asOptional(): Optional<S, A> {
return new Optional(s => some(this.get(s)), a => s => this.reverseGet(a))
}
/** view an Iso as a Traversal */
asTraversal(): Traversal<S, A> {
return new Traversal(<F>(F: Applicative<F>) => (f: (a: A) => HKT<F, A>) => s =>
F.map(a => this.reverseGet(a), f(this.get(s)))
)
}
/** view an Iso as a Fold */
asFold(): Fold<S, A> {
return new Fold(<M>(M: Monoid<M>) => (f: (a: A) => M) => s => f(this.get(s)))
}
/** view an Iso as a Getter */
asGetter(): Getter<S, A> {
return new Getter(s => this.get(s))
}
/** view an Iso as a Setter */
asSetter(): Setter<S, A> {
return new Setter(f => s => this.modify(f)(s))
}
/** compose an Iso with an Iso */
compose<B>(ab: Iso<A, B>): Iso<S, B> {
return new Iso(s => ab.get(this.get(s)), b => this.reverseGet(ab.reverseGet(b)))
}
/** compose an Iso with a Lens */
composeLens<B>(ab: Lens<A, B>): Lens<S, B> {
return this.asLens().compose(ab)
}
/** compose an Iso with a Prism */
composePrism<B>(ab: Prism<A, B>): Prism<S, B> {
return this.asPrism().compose(ab)
}
/** compose an Iso with an Optional */
composeOptional<B>(ab: Optional<A, B>): Optional<S, B> {
return this.asOptional().compose(ab)
}
/** compose an Iso with a Traversal */
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> {
return this.asTraversal().compose(ab)
}
/** compose an Iso with a Fold */
composeFold<B>(ab: Fold<A, B>): Fold<S, B> {
return this.asFold().compose(ab)
}
/** compose an Iso with a Getter */
composeGetter<B>(ab: Getter<A, B>): Getter<S, B> {
return this.asGetter().compose(ab)
}
/** compose an Iso with a Setter */
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> {
return this.asSetter().compose(ab)
}
}
export interface Iso<S, A> {
unwrap: (s: S) => A
to: (s: S) => A
wrap: (a: A) => S
from: (a: A) => S
}
export function lensFromPath<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4],
K6 extends keyof T[K1][K2][K3][K4][K5],
K7 extends keyof T[K1][K2][K3][K4][K5][K6],
K8 extends keyof T[K1][K2][K3][K4][K5][K6][K7],
K9 extends keyof T[K1][K2][K3][K4][K5][K6][K7][K8],
K10 extends keyof T[K1][K2][K3][K4][K5][K6][K7][K8][K9]
>(path: [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10]): Lens<T, T[K1][K2][K3][K4][K5][K6][K7][K8][K9][K10]>
export function lensFromPath<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4],
K6 extends keyof T[K1][K2][K3][K4][K5],
K7 extends keyof T[K1][K2][K3][K4][K5][K6],
K8 extends keyof T[K1][K2][K3][K4][K5][K6][K7],
K9 extends keyof T[K1][K2][K3][K4][K5][K6][K7][K8]
>(path: [K1, K2, K3, K4, K5, K6, K7, K8, K9]): Lens<T, T[K1][K2][K3][K4][K5][K6][K7][K8][K9]>
export function lensFromPath<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4],
K6 extends keyof T[K1][K2][K3][K4][K5],
K7 extends keyof T[K1][K2][K3][K4][K5][K6],
K8 extends keyof T[K1][K2][K3][K4][K5][K6][K7]
>(path: [K1, K2, K3, K4, K5, K6, K7, K8]): Lens<T, T[K1][K2][K3][K4][K5][K6][K7][K8]>
export function lensFromPath<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4],
K6 extends keyof T[K1][K2][K3][K4][K5],
K7 extends keyof T[K1][K2][K3][K4][K5][K6]
>(path: [K1, K2, K3, K4, K5, K6, K7]): Lens<T, T[K1][K2][K3][K4][K5][K6][K7]>
export function lensFromPath<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4],
K6 extends keyof T[K1][K2][K3][K4][K5]
>(path: [K1, K2, K3, K4, K5, K6]): Lens<T, T[K1][K2][K3][K4][K5][K6]>
export function lensFromPath<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4]
>(path: [K1, K2, K3, K4, K5]): Lens<T, T[K1][K2][K3][K4][K5]>
export function lensFromPath<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3]
>(path: [K1, K2, K3, K4]): Lens<T, T[K1][K2][K3][K4]>
export function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(
path: [K1, K2, K3]
): Lens<T, T[K1][K2][K3]>
export function lensFromPath<T, K1 extends keyof T, K2 extends keyof T[K1]>(path: [K1, K2]): Lens<T, T[K1][K2]>
export function lensFromPath<T, K1 extends keyof T>(path: [K1]): Lens<T, T[K1]>
export function lensFromPath(path: Array<any>) {
const lens = Lens.fromProp<any, any>(path[0])
return path.slice(1).reduce((acc, prop) => acc.compose(Lens.fromProp<any, any>(prop)), lens)
}
/*
Laws:
1. get(set(a, s)) = a
2. set(get(s), s) = s
3. set(a, set(a, s)) = set(a, s)
*/
export class Lens<S, A> {
static fromPath = lensFromPath
readonly _tag: 'Lens' = 'Lens'
constructor(readonly get: (s: S) => A, readonly set: (a: A) => (s: S) => S) {}
/** generate a lens from a type and a prop */
static fromProp<T, P extends keyof T>(prop: P): Lens<T, T[P]> {
return new Lens(s => s[prop], a => s => Object.assign({}, s, { [prop as any]: a }))
}
modify(f: (a: A) => A): (s: S) => S {
return s => this.set(f(this.get(s)))(s)
}
/** view a Lens as a Optional */
asOptional(): Optional<S, A> {
return new Optional(s => some(this.get(s)), this.set)
}
/** view a Lens as a Traversal */
asTraversal(): Traversal<S, A> {
return new Traversal(<F>(F: Applicative<F>) => (f: (a: A) => HKT<F, A>) => s =>
F.map(a => this.set(a)(s), f(this.get(s)))
)
}
/** view a Lens as a Setter */
asSetter(): Setter<S, A> {
return new Setter(f => s => this.modify(f)(s))
}
/** view a Lens as a Getter */
asGetter(): Getter<S, A> {
return new Getter(s => this.get(s))
}
/** view a Lens as a Fold */
asFold(): Fold<S, A> {
return new Fold(<M>(M: Monoid<M>) => (f: (a: A) => M) => s => f(this.get(s)))
}
/** compose a Lens with a Lens */
compose<B>(ab: Lens<A, B>): Lens<S, B> {
return new Lens(s => ab.get(this.get(s)), b => s => this.set(ab.set(b)(this.get(s)))(s))
}
/** compose a Lens with a Getter */
composeGetter<B>(ab: Getter<A, B>): Getter<S, B> {
return this.asGetter().compose(ab)
}
/** compose a Lens with a Fold */
composeFold<B>(ab: Fold<A, B>): Fold<S, B> {
return this.asFold().compose(ab)
}
/** compose a Lens with an Optional */
composeOptional<B>(ab: Optional<A, B>): Optional<S, B> {
return this.asOptional().compose(ab)
}
/** compose a Lens with an Traversal */
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> {
return this.asTraversal().compose(ab)
}
/** compose a Lens with an Setter */
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> {
return this.asSetter().compose(ab)
}
/** compose a Lens with an Iso */
composeIso<B>(ab: Iso<A, B>): Lens<S, B> {
return this.compose(ab.asLens())
}
/** compose a Lens with a Prism */
composePrism<B>(ab: Prism<A, B>): Optional<S, B> {
return this.asOptional().compose(ab.asOptional())
}
}
/*
Laws:
1. getOption(s).fold(identity, reverseGet) = s
2. getOption(reverseGet(a)) = Some(a)
*/
export class Prism<S, A> {
readonly _tag: 'Prism' = 'Prism'
constructor(readonly getOption: (s: S) => Option<A>, readonly reverseGet: (a: A) => S) {}
static fromPredicate<A>(predicate: Predicate<A>): Prism<A, A> {
return new Prism(s => (predicate(s) ? some(s) : none), a => a)
}
static some<A>(): Prism<Option<A>, A> {
return somePrism
}
modify(f: (a: A) => A): (s: S) => S {
return s => this.modifyOption(f)(s).fold(constant(s), identity)
}
modifyOption(f: (a: A) => A): (s: S) => Option<S> {
return s => this.getOption(s).map(a => this.reverseGet(f(a)))
}
/** set the target of a Prism with a value */
set(a: A): (s: S) => S {
return this.modify(() => a)
}
/** view a Prism as a Optional */
asOptional(): Optional<S, A> {
return new Optional(this.getOption, a => s => this.reverseGet(a))
}
/** view a Prism as a Traversal */
asTraversal(): Traversal<S, A> {
return new Traversal(<F>(F: Applicative<F>) => (f: (a: A) => HKT<F, A>) => s =>
this.getOption(s).fold(() => F.of(s), a => F.map(fa => this.reverseGet(a), f(a)))
)
}
/** view a Prism as a Setter */
asSetter(): Setter<S, A> {
return new Setter(f => s => this.modify(f)(s))
}
/** view a Prism as a Fold */
asFold(): Fold<S, A> {
return new Fold(<M>(M: Monoid<M>) => (f: (a: A) => M) => s => this.getOption(s).fold(() => M.empty(), f))
}
/** compose a Prism with a Prism */
compose<B>(ab: Prism<A, B>): Prism<S, B> {
return new Prism(s => this.getOption(s).chain(a => ab.getOption(a)), b => this.reverseGet(ab.reverseGet(b)))
}
/** compose a Prism with a Optional */
composeOptional<B>(ab: Optional<A, B>): Optional<S, B> {
return this.asOptional().compose(ab)
}
/** compose a Prism with a Traversal */
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> {
return this.asTraversal().compose(ab)
}
/** compose a Prism with a Fold */
composeFold<B>(ab: Fold<A, B>): Fold<S, B> {
return this.asFold().compose(ab)
}
/** compose a Prism with a Setter */
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> {
return this.asSetter().compose(ab)
}
/** compose a Prism with a Iso */
composeIso<B>(ab: Iso<A, B>): Prism<S, B> {
return this.compose(ab.asPrism())
}
/** compose a Prism with a Lens */
composeLens<B>(ab: Lens<A, B>): Optional<S, B> {
return this.asOptional().compose(ab.asOptional())
}
/** compose a Prism with a Getter */
composeGetter<B>(ab: Getter<A, B>): Fold<S, B> {
return this.asFold().compose(ab.asFold())
}
}
const somePrism = new Prism<Option<never>, never>(s => s, a => some(a))
/*
Laws:
1. getOption(s).fold(() => s, a => set(a)(s)) = s
2. getOption(set(a)(s)) = getOption(s).map(_ => a)
3. set(a)(set(a)(s)) = set(a)(s)
*/
export class Optional<S, A> {
readonly _tag: 'Optional' = 'Optional'
constructor(readonly getOption: (s: S) => Option<A>, readonly set: (a: A) => (s: S) => S) {}
modify(f: (a: A) => A): (s: S) => S {
return s => this.modifyOption(f)(s).fold(constant(s), identity)
}
modifyOption(f: (a: A) => A): (s: S) => Option<S> {
return s => this.getOption(s).map(a => this.set(f(a))(s))
}
/** view a Optional as a Traversal */
asTraversal(): Traversal<S, A> {
return new Traversal(<F>(F: Applicative<F>) => (f: (a: A) => HKT<F, A>) => s =>
this.getOption(s).fold(() => F.of(s), a => F.map((a: A) => this.set(a)(s), f(a)))
)
}
/** view an Optional as a Fold */
asFold(): Fold<S, A> {
return new Fold(<M>(M: Monoid<M>) => (f: (a: A) => M) => s => this.getOption(s).fold(() => M.empty(), f))
}
/** view an Optional as a Setter */
asSetter(): Setter<S, A> {
return new Setter(f => s => this.modify(f)(s))
}
/** compose a Optional with a Optional */
compose<B>(ab: Optional<A, B>): Optional<S, B> {
return new Optional<S, B>(
s => this.getOption(s).chain(a => ab.getOption(a)),
b => s => this.modify(a => ab.set(b)(a))(s)
)
}
/** compose an Optional with a Traversal */
composeTraversal<B>(ab: Traversal<A, B>): Traversal<S, B> {
return this.asTraversal().compose(ab)
}
/** compose an Optional with a Fold */
composeFold<B>(ab: Fold<A, B>): Fold<S, B> {
return this.asFold().compose(ab)
}
/** compose an Optional with a Setter */
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> {
return this.asSetter().compose(ab)
}
/** compose an Optional with a Lens */
composeLens<B>(ab: Lens<A, B>): Optional<S, B> {
return this.compose(ab.asOptional())
}
/** compose an Optional with a Prism */
composePrism<B>(ab: Prism<A, B>): Optional<S, B> {
return this.compose(ab.asOptional())
}
/** compose an Optional with a Iso */
composeIso<B>(ab: Iso<A, B>): Optional<S, B> {
return this.compose(ab.asOptional())
}
/** compose an Optional with a Getter */
composeGetter<B>(ab: Getter<A, B>): Fold<S, B> {
return this.asFold().compose(ab.asFold())
}
}
export class Traversal<S, A> {
readonly _tag: 'Traversal' = 'Traversal'
constructor(
// Van Laarhoven representation
readonly modifyF: <F>(F: Applicative<F>) => (f: (a: A) => HKT<F, A>) => (s: S) => HKT<F, S>
) {}
modify(f: (a: A) => A): (s: S) => S {
return s => (this.modifyF(id)(a => id.of(f(a)))(s) as id.Identity<S>).extract()
}
set(a: A): (s: S) => S {
return s => this.modify(constant(a))(s)
}
/** view a Traversal as a Fold */
asFold(): Fold<S, A> {
return new Fold(<M>(M: Monoid<M>) => (f: (a: A) => M) => s =>
(this.modifyF(const_.getApplicative(M))(a => new const_.Const(f(a)))(s) as const_.Const<M, S>).fold(identity)
)
}
/** view a Traversal as a Setter */
asSetter(): Setter<S, A> {
return new Setter(f => s => this.modify(f)(s))
}
/** compose a Traversal with a Traversal */
compose<B>(ab: Traversal<A, B>): Traversal<S, B> {
return new Traversal<S, B>(<F>(F: Applicative<F>) => (f: (a: B) => HKT<F, B>) => (s: S): HKT<F, S> =>
this.modifyF(F)(a => ab.modifyF(F)(f)(a))(s)
)
}
/** compose a Traversal with a Fold */
composeFold<B>(ab: Fold<A, B>): Fold<S, B> {
return this.asFold().compose(ab)
}
/** compose a Traversal with a Setter */
composeSetter<B>(ab: Setter<A, B>): Setter<S, B> {
return this.asSetter().compose(ab)
}
/** compose a Traversal with a Optional */
composeOptional<B>(ab: Optional<A, B>): Traversal<S, B> {
return this.compose(ab.asTraversal())
}
/** compose a Traversal with a Lens */
composeLens<B>(ab: Lens<A, B>): Traversal<S, B> {
return this.compose(ab.asTraversal())
}
/** compose a Traversal with a Prism */
composePrism<B>(ab: Prism<A, B>): Traversal<S, B> {
return this.compose(ab.asTraversal())
}
/** compose a Traversal with a Iso */
composeIso<B>(ab: Iso<A, B>): Traversal<S, B> {
return this.compose(ab.asTraversal())
}
/** compose a Traversal with a Getter */
composeGetter<B>(ab: Getter<A, B>): Fold<S, B> {
return this.asFold().compose(ab.asFold())
}
}
export class Getter<S, A> {
readonly _tag: 'Getter' = 'Getter'
constructor(readonly get: (s: S) => A) {}
/** view a Getter as a Fold */
asFold(): Fold<S, A> {
return new Fold(<M>(M: Monoid<M>) => (f: (a: A) => M) => s => f(this.get(s)))
}
/** compose a Getter with a Getter */
compose<B>(ab: Getter<A, B>): Getter<S, B> {
return new Getter(s => ab.get(this.get(s)))
}
/** compose a Getter with a Fold */
composeFold<B>(ab: Fold<A, B>): Fold<S, B> {
return this.asFold().compose(ab)
}
/** compose a Getter with a Lens */
composeLens<B>(ab: Lens<A, B>): Getter<S, B> {
return this.compose(ab.asGetter())
}
/** compose a Getter with a Iso */
composeIso<B>(ab: Iso<A, B>): Getter<S, B> {
return this.compose(ab.asGetter())
}
/** compose a Getter with a Optional */
composeTraversal<B>(ab: Traversal<A, B>): Fold<S, B> {
return this.asFold().compose(ab.asFold())
}
/** compose a Getter with a Optional */
composeOptional<B>(ab: Optional<A, B>): Fold<S, B> {
return this.asFold().compose(ab.asFold())
}
/** compose a Getter with a Prism */
composePrism<B>(ab: Prism<A, B>): Fold<S, B> {
return this.asFold().compose(ab.asFold())
}
}
export class Fold<S, A> {
readonly _tag: 'Fold' = 'Fold'
private foldMapFirst: (f: (a: A) => Option<A>) => (s: S) => Option<A>
constructor(readonly foldMap: <M>(M: Monoid<M>) => (f: (a: A) => M) => (s: S) => M) {
this.getAll = foldMap(monoidArray)(a => [a])
this.exist = foldMap(monoidAny)
this.all = foldMap(monoidAll)
this.foldMapFirst = foldMap(option.getFirstMonoid())
}
/** compose a Fold with a Fold */
compose<B>(ab: Fold<A, B>): Fold<S, B> {
return new Fold(<M>(M: Monoid<M>) => (f: (b: B) => M) => (s: S): M => this.foldMap(M)(a => ab.foldMap(M)(f)(a))(s))
}
/** compose a Fold with a Getter */
composeGetter<B>(ab: Getter<A, B>): Fold<S, B> {
return this.compose(ab.asFold())
}
/** compose a Fold with a Traversal */
composeTraversal<B>(ab: Traversal<A, B>): Fold<S, B> {
return this.compose(ab.asFold())
}
/** compose a Fold with a Optional */
composeOptional<B>(ab: Optional<A, B>): Fold<S, B> {
return this.compose(ab.asFold())
}
/** compose a Fold with a Lens */
composeLens<B>(ab: Lens<A, B>): Fold<S, B> {
return this.compose(ab.asFold())
}
/** compose a Fold with a Prism */
composePrism<B>(ab: Prism<A, B>): Fold<S, B> {
return this.compose(ab.asFold())
}
/** compose a Fold with a Iso */
composeIso<B>(ab: Iso<A, B>): Fold<S, B> {
return this.compose(ab.asFold())
}
/** find the first target of a Fold matching the predicate */
find(p: Predicate<A>): (s: S) => Option<A> {
return this.foldMapFirst(a => (p(a) ? option.of(a) : option.none))
}
/** get the first target of a Fold */
headOption(s: S): Option<A> {
return this.find(() => true)(s)
}
}
export interface Fold<S, A> {
/** get all the targets of a Fold */
getAll(s: S): Array<A>
/** check if at least one target satisfies the predicate */
exist(p: Predicate<A>): Predicate<S>
/** check if all targets satisfy the predicate */
all(p: Predicate<A>): Predicate<S>
}
export class Setter<S, A> {
readonly _tag: 'Setter' = 'Setter'
constructor(readonly modify: (f: (a: A) => A) => (s: S) => S) {}
set(a: A): (s: S) => S {
return s => this.modify(constant(a))(s)
}
/** compose a Setter with a Setter */
compose<B>(ab: Setter<A, B>): Setter<S, B> {
return new Setter(f => s => this.modify((a: A) => ab.modify(f)(a))(s))
}
/** compose a Setter with a Traversal */
composeTraversal<B>(ab: Traversal<A, B>): Setter<S, B> {
return this.compose(ab.asSetter())
}
/** compose a Setter with a Optional */
composeOptional<B>(ab: Optional<A, B>): Setter<S, B> {
return this.compose(ab.asSetter())
}
/** compose a Setter with a Lens */
composeLens<B>(ab: Lens<A, B>): Setter<S, B> {
return this.compose(ab.asSetter())
}
/** compose a Setter with a Prism */
composePrism<B>(ab: Prism<A, B>): Setter<S, B> {
return this.compose(ab.asSetter())
}
/** compose a Setter with a Iso */
composeIso<B>(ab: Iso<A, B>): Setter<S, B> {
return this.compose(ab.asSetter())
}
}
export class Ops {
/** create a Traversal from a Traversable */
fromTraversable<T extends HKT2S>(T: Traversable<T>): <L, A>() => Traversal<HKT2As<T, L, A>, A>
fromTraversable<T extends HKTS>(T: Traversable<T>): <A>() => Traversal<HKTAs<T, A>, A>
fromTraversable<T>(T: Traversable<T>): <A>() => Traversal<HKT<T, A>, A>
fromTraversable<T>(T: Traversable<T>): <A>() => Traversal<HKT<T, A>, A> {
return <A>() =>
new Traversal<HKT<T, A>, A>(<F>(F: Applicative<F>) => (f: (a: A) => HKT<F, A>) => s => T.traverse(F)(f, s))
}
/** create a Fold from a Foldable */
fromFoldable<F extends HKT2S>(F: Foldable<F>): <L, A>() => Fold<HKT2As<F, L, A>, A>
fromFoldable<F extends HKTS>(F: Foldable<F>): <A>() => Fold<HKTAs<F, A>, A>
fromFoldable<F>(F: Foldable<F>): <A>() => Fold<HKT<F, A>, A>
fromFoldable<F>(F: Foldable<F>): <A>() => Fold<HKT<F, A>, A> {
return <A>() => new Fold<HKT<F, A>, A>(<M>(M: Monoid<M>) => (f: (a: A) => M) => s => foldMap(F, M)(f)(s))
}
}
const ops = new Ops()
export const fromTraversable: Ops['fromTraversable'] = ops.fromTraversable
export const fromFoldable: Ops['fromFoldable'] = ops.fromFoldable