forked from baetheus/fun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptics.ts
More file actions
1589 lines (1526 loc) · 43.9 KB
/
optics.ts
File metadata and controls
1589 lines (1526 loc) · 43.9 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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Optics are a collection of combinators for focusing on specific parts of data
* within an existing structure. The core operations are view, review, and
* modify. Optics in the fun library are based on the concept of Kliesli optics
* as outlined
* [here](https://gist.github.com/serras/5152ec18ec5223b676cc67cac0e99b70).
*
* Their uses include (but are not limited to):
* * Accessing deeply nested data
* * Mapping related but distinct types without loss of fidelity
* * Immutably modifying large data structures
*
* At its core, instead of separating the view method into view, preview, and
* toList of, as is done in many languages and libraries. This implementation
* uses a single view method that operates as a Kliesli arrow (a -> mb) where
* the m in this case is limited to the Identity, Option, and Array monads,
* which can be composed using Natural transformations and flatMap.
*
* In addition to view, there are also implementations of review and modify,
* which also have composition functions.. but the research for composition is
* not yet complete for review.
*
* In any case, this implementation of optics is distinct from Laarhoven lenses
* and profunctor optics, and is much more compact and performant in typescript
* than those implementations.
*
* @module Optics
*
* @since 2.0.0
*/
import type { $, Kind } from "./kind.ts";
import type { ReadonlyRecord } from "./record.ts";
import type { Tree } from "./tree.ts";
import type { Either } from "./either.ts";
import type { Monad } from "./monad.ts";
import type { Monoid } from "./monoid.ts";
import type { Option } from "./option.ts";
import type { Pair } from "./pair.ts";
import type { Predicate } from "./predicate.ts";
import type { Refinement } from "./refinement.ts";
import type { Eq } from "./eq.ts";
import type { Traversable } from "./traversable.ts";
import * as I from "./identity.ts";
import * as O from "./option.ts";
import * as A from "./array.ts";
import * as R from "./record.ts";
import * as E from "./either.ts";
import * as M from "./map.ts";
import * as P from "./pair.ts";
import { TraversableSet } from "./set.ts";
import { TraversableTree } from "./tree.ts";
import { isNotNil } from "./nilable.ts";
import { concatAll as getConcatAll } from "./monoid.ts";
import { dimap, flow, identity, over, pipe } from "./fn.ts";
/**
* A runtime tag and type that indicates an Optic has a view function of the form
* `(s: S) => Identity<A>`.
*/
export const LensTag = "Lens" as const;
export type LensTag = typeof LensTag;
/**
* A runtime tag and type that indicates an Optic has a view function of the form
* `(s: S) => Option<A>`.
*/
export const AffineTag = "Affine" as const;
export type AffineTag = typeof AffineTag;
/**
* A runtime tag and type that indicates an Optic has a view function of the form
* `(s: S) => ReadonlyArray<A>`.
*/
export const FoldTag = "Fold" as const;
export type FoldTag = typeof FoldTag;
/**
* A type union of the supported view tags for a Viewer
*/
export type Tag = LensTag | AffineTag | FoldTag;
/**
* A type level mapping from an Optic Tag to its associated output Kind. This is
* used to substitute the container of the output of a view function.
*/
type ToKind<T extends Tag> = T extends LensTag ? I.KindIdentity
: T extends AffineTag ? O.KindOption
: T extends FoldTag ? A.KindArray
: never;
/**
* A type level computation of Optic Tags. When composing the view functions of
* two optics their output types must be aligned. This type aligns their tags at
* the type level. It has a corresponding runtime align function.
*/
type Align<U extends Tag, V extends Tag> = U extends FoldTag ? FoldTag
: V extends FoldTag ? FoldTag
: U extends AffineTag ? AffineTag
: V extends AffineTag ? AffineTag
: LensTag;
/**
* A runtime computation over Optic Tags. When composing two optics, the output
* of their view functions must be aligned. This does that aligning. In general,
* it operates like the Math.max function but for LensTag, AffineTag, and
* FoldTag. Specifically, FoldTag > AffineTag > LensTag.
*/
function align<A extends Tag, B extends Tag>(
a: A,
b: B,
): Align<A, B> {
return ((a === FoldTag || b === FoldTag)
? FoldTag
: (a === AffineTag || b === AffineTag)
? AffineTag
: LensTag) as Align<A, B>;
}
/**
* Given a Viewer<U, S, A> and an optic tag V, this function produces a view
* function that can be used in a Viewer<V, S, A>. However, not all casts are valid.
* The following are the only supported casts, by their optic tag:
*
* * LensTag => LensTag
* * LensTag => AffineTag
* * LensTag => FoldTag
* * AffineTag => AffineTag
* * AffineTag => FoldTag
* * FoldTag => FoldTag
*
* The following are unsupported casts which will throw at runtime:
*
* * AffineTag => LensTag
* * FoldTag => AffineTag
* * FoldTag => LensTag
*
* This library has no code that leads to unsupported casts, but if one wishes
* to extend its functionality by replicating the cast logic, these cases must
* be considered.
*/
export function _unsafeCast<U extends Tag, V extends Tag, S, A>(
viewer: Viewer<U, S, A>,
tag: V,
): Viewer<V, S, A>["view"] {
type Out = Viewer<V, S, A>["view"];
// Covers Lens => Lens, AffineFold => AffineFold, Fold => Fold
if (viewer.tag === tag as LensTag) {
return viewer.view as Out;
// AffineFold => Fold
} else if (tag === FoldTag && viewer.tag === AffineTag) {
return (s: S) => {
const ua = viewer.view(s) as Option<A>;
return (O.isNone(ua) ? [] : [ua.value]) as ReturnType<Out>;
};
// Lens => Fold
} else if (tag === FoldTag && viewer.tag === LensTag) {
return (s: S) => [viewer.view(s)] as ReturnType<Out>;
// Lens => AffineFold
} else if (tag === AffineTag && viewer.tag == LensTag) {
return (s) => O.of(viewer.view(s)) as ReturnType<Out>;
}
// Non-valid casts will throw an error at runtime.
// This is not reachable with the combinators in this lib.
throw new Error(`Attempted to cast ${viewer.tag} to ${tag}`);
}
/**
* Recover a Monad from an Optic Tag. The following cases are handled:
*
* * LensTag => MonadIdentity
* * AffineTag => MonadOption
* * FoldTag => MonadArray
*/
function getMonad<T extends Tag>(tag: T): Monad<ToKind<T>> {
return (tag === FoldTag
? A.MonadArray
: tag === AffineTag
? O.MonadOption
: I.MonadIdentity) as unknown as Monad<ToKind<T>>;
}
/**
* A Viewer<T, S, A> implements a view function `(s: S) => T<A>`. This is
* effectively a Kliesli Arrow. The valid types of T are Identity, Option, and
* Array. Viewer also includes a runtime tag corresponding to the return type of
* the view function to aid in composition.
*
* @since 2.0.0
*/
export interface Viewer<T extends Tag, S, A> {
readonly tag: T;
readonly view: (s: S) => $<ToKind<T>, [A, never, never]>;
}
/**
* The Modifier<S, A> type implements the modify function
* `(mod: (a: A) => A) => (s: S) => S`. This type is directly composable and
* from it one can recover set/replace behavior.
*
* @since 2.0.0
*/
export interface Modifier<S, A> {
readonly modify: (modifyFn: (a: A) => A) => (s: S) => S;
}
/**
* The Reviewer<S, A. type implements the review function `(a: A) => S`. This
* type is directly composable and is used when the S type in Viewer<U, S, A>
* can be reconstructed from A. Some examples are constructing `Option<number>`
* from `number`, `Array<number>` from `number`, etc.
*
* @since 2.0.0
*/
export interface Reviewer<S, A> {
readonly review: (a: A) => S;
}
/**
* An Optic<T, S, A> is defined as a Viewer<T, S, A> combined with a Modifier<S,
* A>. This is the root type for the specific types of Optics defined below.
*
* @since 2.0.0
*/
export interface Optic<T extends Tag, S, A>
extends Viewer<T, S, A>, Modifier<S, A> {}
/**
* Lens<S, A> is an alias of Optic<LensTag, S, A>. This means that the view
* function of a Lens returns a pure A value. `(s: S) => A`. Some example lenses
* are accessing the property of a struct, accessing the first value in a Pair,
* and the trivial identity Lens. In general, a Lens is used for focusing on
* *exactly one* value `A` contained in the value `S`.
*
* @since 2.0.0
*/
export type Lens<S, A> = Optic<LensTag, S, A>;
/**
* Iso<S, A> is an alias of Lens<S, A> & Reviewer<S, A>. This means that an Iso
* operates exactly like a Lens with the added ability to go back from A to S.
*
* @since 2.0.0
*/
export type Iso<S, A> = Lens<S, A> & Reviewer<S, A>;
/**
* AffineFold<S, A> is an alias of Optic<AffineTag, S, A>. This means the view
* function of an AffineFold returns an Option<A>, `(s: S) => Option<A>`. Some
* example AffineFolds are accessing a value at an index in an array, accessing
* the value in an Option<A>, and accessing a key in a Record type. In general,
* an AffineFold is used for focusing on *zero or one* value `A` contained in
* the value `S`.
*
* @since 2.0.0
*/
export type AffineFold<S, A> = Optic<AffineTag, S, A>;
/**
* Prism<S, A> is an alias of AffineFold<S, A> & Reviewer<S, A>. This means that
* a Prism operates exactly like an AffineFold with the added ability to
* *reconstruct* an S from an A. Examples of this are reconstructing an
* Option<number> from a number, or reconstructing Either<string, number> from a
* string or a number.
*
* @since 2.0.0
*/
export type Prism<S, A> = AffineFold<S, A> & Reviewer<S, A>;
/**
* Fold<S, A> is an alias of Optic<FoldTag, S, A>. This means that the view
* function of a Fold returns a ReadonlyArray<A>, `(s: S) => ReadonlyArray<A>`.
* Some example Folds are accessing all of the values in a Record, Tree, Set,
* etc. In general, a Fold is used for focusing on *any number* of values `A`
* contained in the value `S`.
*
* @since 2.0.0
*/
export type Fold<S, A> = Optic<FoldTag, S, A>;
/**
* Refold<S, A> is an alias of Fold<S, A> & Reviewer<S, A>. This means that a
* Refold operates exactly like a Fold with the added ability to *reconstruct*
* an S from a single value A. Examples of this are reconstructing an Array<A>
* from a value A, or reconstructing a Tree<A> from a value A.
*
* @since 2.0.0
*/
export type Refold<S, A> = Fold<S, A> & Reviewer<S, A>;
/**
* Construct a Viewer<T, S, A> from a tag T and a view function that matches
* said tag. This is a raw constructor and is generally only useful if there is
* a case where a structure can be lensed into but not reconstructed or
* traversed. However, this is the core composable structure of optics, as they
* are primarily meant as a way to retrieve data from an existing structure.
*
* @example The "Lens" viewer retrieves a single value that always exists.
* ```ts
* import type { Pair } from "./pair.ts";
*
* import * as O from "./optics.ts";
* import * as P from "./pair.ts";
*
* const fst = <A, B = unknown>() => O.viewer<O.LensTag, Pair<A, B>, A>(
* O.LensTag,
* P.getFirst,
* );
*
* const numPair = fst<number, number>();
*
* const result1 = numPair.view(P.pair(1, 2)); // 1
* const result2 = numPair.view(P.pair(2, 1)); // 2
* ```
*
* @example The "Affine" viewer retrieves a single value that might exists.
* ```ts
* import type { Either } from "./either.ts";
*
* import * as O from "./optics.ts";
* import * as E from "./either.ts";
*
* const right = <R, L = unknown>() => O.viewer<O.AffineTag, Either<L, R>, R>(
* O.AffineTag,
* E.getRight,
* );
*
* const numberEither = right<number, string>();
*
* const result1 = numberEither.view(E.right(1)); // Some(1)
* const result2 = numberEither.view(E.left("Hello")); // None
* ```
*
* @example The "Fold" viewer retrieves zero or more values as an Array.
* ```ts
* import * as O from "./optics.ts";
*
* const record = <A>() => O.viewer<O.FoldTag, Record<string, A>, A>(
* O.FoldTag,
* Object.values,
* );
*
* const numberRecord = record<number>();
*
* const result = numberRecord.view({
* "one": 1,
* "two": 2,
* }); // [1, 2]
* ```
*
* @since 2.0.0
*/
export function viewer<T extends Tag, S, A>(
tag: T,
view: (s: S) => $<ToKind<T>, [A, never, never]>,
): Viewer<T, S, A> {
return { tag, view };
}
/**
* Construct a Modifier<S, A. from a modify function.
*
* @since 2.0.0
*/
export function modifier<S, A>(
modify: (modifyFn: (a: A) => A) => (s: S) => S,
): Modifier<S, A> {
return { modify };
}
/**
* Construct a Reviewer<S, A> from a review function.
*
* @since 2.0.0
*/
export function reviewer<S, A>(review: (a: A) => S): Reviewer<S, A> {
return { review };
}
/**
* Construct an Optic<U, S, A> & Reviewer<S, A> from a tag as well as view,
* modify, and reivew functions.
*
* @since 2.0.0
*/
export function optic<U extends Tag, S, A>(
tag: U,
view: (s: S) => $<ToKind<U>, [A, never, never]>,
modify: (modifyFn: (a: A) => A) => (s: S) => S,
review: (a: A) => S,
): Optic<U, S, A> & Reviewer<S, A>;
/**
* Construct an Optic<U, S, A> from a tag as well as view and modify functions.
*
* @since 2.0.0
*/
export function optic<U extends Tag, S, A>(
tag: U,
view: (s: S) => $<ToKind<U>, [A, never, never]>,
modify: (modifyFn: (a: A) => A) => (s: S) => S,
): Optic<U, S, A>;
export function optic<U extends Tag, S, A>(
tag: U,
view: (s: S) => $<ToKind<U>, [A, never, never]>,
modify: (modifyFn: (a: A) => A) => (s: S) => S,
review?: (a: A) => S,
): Optic<U, S, A> | Optic<U, S, A> & Reviewer<S, A> {
return typeof review === "function"
? { tag, view, modify, review }
: { tag, view, modify };
}
/**
* Construct a Lens<S, A> from view and modify functions.
*
* @example
* ```ts
* import type { NonEmptyArray } from "./array.ts";
*
* import * as O from "./optics.ts";
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const head = <A>() => O.lens<NonEmptyArray<A>, A>(
* ([head]) => head,
* mod => ([head, ...rest]) => [mod(head), ...rest],
* );
*
* const headNum = head<number>();
*
* const result1 = headNum.view(A.array(1, 2, 3)); // 1
* const result2 = headNum.modify((n: number) => n + 100)(A.array(1, 2, 3));
* // [100, 2, 3]
* ```
*
* @since 2.0.0
*/
export function lens<S, A>(
view: (s: S) => A,
modify: (modifyFn: (a: A) => A) => (s: S) => S,
): Lens<S, A> {
return optic(LensTag, view, modify);
}
/**
* Construct an Iso<S, A> from view and review functions, with an optional
* modify function if it is different from
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import { Option, match, some, none, map, fromPredicate } from "./option.ts";
* import { pipe, identity } from "./fn.ts";
*
* const { view, review, modify }: O.Iso<Option<number>, number> = O.iso(
* match(() => 0, identity),
* fromPredicate(n => n !== 0),
* map,
* );
*
* const result1 = view(some(1)); // 1
* const result2 = view(none); // 0
* const result3 = review(1); // Some(1)
* const result4 = review(0); // None
* const result5 = modify(n => n + 100)(some(1)); // Some(101)
* const result6 = modify(n => n + 100)(none); // Some(100)
* ```
*
* @since 2.0.0
*/
export function iso<S, A>(
view: (s: S) => A,
review: (a: A) => S,
modify: (modifyFn: (a: A) => A) => (s: S) => S = dimap(view, review),
): Iso<S, A> {
return optic(LensTag, view, modify, review);
}
/**
* Construct an AffineFold<S, A> from view and modify functions.
*
* @example
* ```ts
* import type { Either } from "./either.ts";
*
* import * as O from "./optics.ts";
* import * as E from "./either.ts";
*
* const right = <R, L = unknown>() => O.affineFold<Either<L, R>, R>(
* E.getRight,
* E.map,
* );
*
* const numberRight = right<number, string>();
*
* const result1 = numberRight.view(E.right(1)); // Some(1)
* const result2 = numberRight.view(E.left("Hello")); // None
* const result3 = numberRight.modify(n => n + 1)(E.right(1)); // Right(2)
* const result4 = numberRight.modify(n => n + 1)(E.left("Hello"));
* // Left("Hello")
* ```
*
* @since 2.0.0
*/
export function affineFold<S, A>(
view: (s: S) => Option<A>,
modify: (modifyFn: (a: A) => A) => (s: S) => S,
): AffineFold<S, A> {
return optic(AffineTag, view, modify);
}
/**
* Construct a Prism<S, A> from view and review functions, with an optional
* modify function that will be defaulted if not provided.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import * as Op from "./option.ts";
*
* const key = (key: string) => <A>() =>
* O.prism<Record<string, A>, A>(
* rec => Op.fromNullable(rec[key]),
* a => ({ [key]: a }),
* mod => s => Object.hasOwn(s, key) ? ({ ...s, [key]: mod(s[key]) }) : s,
* );
*
* const atFoo = key("foo")<number>();
*
* const result1 = atFoo.view({ bar: 1 }); // None
* const result2 = atFoo.view({ foo: 2 }); // Some(2)
* const result3 = atFoo.review(5); // { foo: 5 }
* const result4 = atFoo.modify(n => n + 1)({ bar: 1 }); // { bar: 1 }
* const result5 = atFoo.modify(n => n + 1)({ foo: 1 }); // { foo: 2 }
* const result6 = atFoo.modify(n => n + 1)({ foo: 1, bar: 2 });
* // { foo: 2, bar: 2 }
* ```
*
* @since 2.0.0
*/
export function prism<S, A>(
view: (s: S) => Option<A>,
review: (a: A) => S,
modify: (modifyFn: (a: A) => A) => (s: S) => S = (modifyFn) => (s) =>
pipe(view(s), O.map(modifyFn), O.match(() => s, review)),
): Prism<S, A> {
return optic(AffineTag, view, modify, review);
}
/**
* Construct a Fold<S, A> from view and modify functions.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import * as R from "./record.ts";
*
* const values = <A>() => O.fold<Record<string, A>, A>(
* Object.values,
* R.map,
* );
*
* const numberValues = values<number>();
*
* const result1 = numberValues.view({}); // []
* const result2 = numberValues.view({ foo: 1 }); // [1]
* const result3 = numberValues.modify(n => n + 1)({}); // {}
* const result4 = numberValues.modify(n => n + 1)({ foo: 1 }); // { foo: 2 }
* ```
*
* @since 2.0.0
*/
export function fold<S, A>(
view: (s: S) => ReadonlyArray<A>,
modify: (modifyFn: (a: A) => A) => (s: S) => S,
): Fold<S, A> {
return optic(FoldTag, view, modify);
}
/**
* Construct a Refold<S, A> from view, review, and modify functions.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import * as S from "./set.ts";
*
* const set = <A>() => O.refold<ReadonlySet<A>, A>(
* Array.from,
* S.of,
* S.map,
* );
*
* const numberSet = set<number>();
*
* const result1 = numberSet.view(S.set(1, 2, 3)); // [1, 2, 3]
* const result2 = numberSet.view(S.empty()); // []
* const result3 = numberSet.review(1); // Set(1)
* const result4 = numberSet.modify(n => n + 1)(S.of(1)); // Set(2)
* ```
*
* @since 2.0.0
*/
export function refold<S, A>(
view: (s: S) => ReadonlyArray<A>,
review: (a: A) => S,
modify: (modifyFn: (a: A) => A) => (s: S) => S,
): Refold<S, A> {
return { tag: FoldTag, view, review, modify };
}
/**
* Construct a Prism<S, A> from a Refinement<S, A>.
*
* @example
* ```ts
* import type { NonEmptyArray } from "./array.ts";
*
* import * as O from "./optics.ts";
*
* const isNonEmpty = <A>(arr: ReadonlyArray<A>): arr is NonEmptyArray<A> =>
* arr.length > 0;
* const nonempty = O.fromPredicate(isNonEmpty<number>);
*
* const result1 = nonempty.view([]); // None
* const result2 = nonempty.view([1]); // Some([1]) as NonEmptyArray
* const result3 = nonempty.review([1]); // [1] Cast NonEmptyArray as Array
* ```
*
* @since 2.0.0
*/
export function fromPredicate<S, A extends S>(
refinement: Refinement<S, A>,
): Prism<S, A>;
/**
* Construct a Prism<A, A> from a Predicate<A, A>.
*
* @example
* ```ts
* import * as O from "./optics.ts";
*
* const positive = O.fromPredicate((n: number) => n > 0);
*
* const result1 = positive.view(1); // Some(1)
* const result2 = positive.view(0); // None
* const result3 = positive.review(0); // 0
* const result4 = positive.modify(n => n + 1)(0); // 0
* const result5 = positive.modify(n => n + 1)(1); // 2
* ```
*
* @since 2.0.0
*/
export function fromPredicate<A>(predicate: Predicate<A>): Prism<A, A>;
export function fromPredicate<A>(predicate: Predicate<A>): Prism<A, A> {
return prism(O.fromPredicate(predicate), identity);
}
/**
* A pipeable view function that applies a value S to a Viewer<S, A>. It will
* return either a raw value, an option, or a readonlyarray based on the tag of
* the Viewer. Note: All Optics are Viewers.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import { pipe } from "./fn.ts";
*
* type Foo = { readonly bar: number };
*
* const bar = pipe(O.id<Foo>(), O.prop("bar"));
*
* const result = pipe(bar, O.view({ bar: 1 })); // 1
* ```
*
* @since 2.0.0
*/
export function view<S>(
s: S,
): <U extends Tag, A>(
viewer: Viewer<U, S, A>,
) => ReturnType<typeof viewer.view> {
return (viewer) => viewer.view(s);
}
/**
* A pipeable modify function that applies a modification function to a
* Modifier<S, A> modify function. It will return a function S -> S that applies
* the modify function according to the type of optic. Note: All Optics are
* Modifiers.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import { pipe } from "./fn.ts";
*
* type Person = { readonly name: string };
*
* const name = pipe(O.id<Person>(), O.prop("name"));
*
* const upper = pipe(name, O.modify(s => s.toUpperCase()));
*
* const result1 = upper({ name: "brandon" }); // { name: "BRANDON" }
* ```
*
* @since 2.0.0
*/
export function modify<A>(faa: (a: A) => A): <S>(
modifier: Modifier<S, A>,
) => ReturnType<typeof modifier.modify> {
return (modifier) => modifier.modify(faa);
}
/**
* A pipeable replace function, that uses the modify function of an Optic to
* replace an existing value over the structure S.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import { pipe } from "./fn.ts";
*
* type Person = { name: string };
*
* const name = pipe(O.id<Person>(), O.prop("name"));
* const toBrandon = pipe(name, O.replace("Brandon"));
*
* const tina: Person = { name: "Tina" }
*
* const result = toBrandon(tina); // { name: "Brandon" }
* ```
*
* @since 2.0.0
*/
export function replace<A>(a: A): <S>(
modifier: Modifier<S, A>,
) => (s: S) => S {
const value = () => a;
return (modifier) => modifier.modify(value);
}
/**
* A pipeable review function that applies a value A to the the review function
* of a Reviewer<S, A>. It returns a value S.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import * as S from "./set.ts";
* import { pipe } from "./fn.ts";
*
* const numberSet = O.refold<ReadonlySet<number>, number>(
* Array.from,
* S.of,
* S.map,
* );
*
* const result = pipe(numberSet, O.review(1)); // ReadonlySet(1)
* ```
*
* @since 2.0.0
*/
export function review<A>(a: A): <S>(reviewer: Reviewer<S, A>) => S {
return (reviewer) => reviewer.review(a);
}
/**
* All id functions for Optics are satisfied by iso(identity, identity). Thus,
* we construct a singleton to cut down on computation. Generally, this function
* will be inlined.
*/
// deno-lint-ignore no-explicit-any
const _identity: Iso<any, any> = iso(identity, identity);
/**
* Construct an Iso<A, A> from a type level argument. This is the entrypoint to
* almost all optics as it allows one to start with a type and compose other
* optics from there.
*
* @example
* ```ts
* import * as O from "./optics.ts";
*
* const number = O.id<number>();
*
* const result1 = number.view(1); // 1
* const result2 = number.review(1); // 1
* const result4 = number.modify(n => n + 1)(1); // 2
* ```
*
* @since 2.0.0
*/
export function id<A>(): Iso<A, A> {
return _identity;
}
/**
* Compose two optics, aligning their tag and building the composition using
* natural transformations and monadic chaining for the view function and using
* direct composition for the modify function.
*
* The general algorithm for optic composition in fun is:
*
* 1. Finding the alignment of them, which is Max<first, second> where
* Fold > AffineFold > Get
* 2. Cast both optics to the alignment tag, one cast will always be
* a noop.
* 3. Construct a new optic by chaining the view functions first to
* second and composing the modify functions second to first.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import { pipe } from "./fn.ts";
*
* const even = O.fromPredicate((n: number) => n % 2 === 0);
* const positive = O.fromPredicate((n: number) => n > 0);
*
* const evenPos = pipe(
* even,
* O.compose(positive),
* );
* const addTwo = pipe(evenPos, O.modify(n => n + 2));
*
* const result1 = pipe(evenPos, O.view(0)); // None
* const result2 = pipe(evenPos, O.view(1)); // None
* const result3 = pipe(evenPos, O.view(2)); // Some(2)
* const result4 = addTwo(0); // 0
* const result5 = addTwo(1); // 1
* const result6 = addTwo(2); // 2
* ```
*
* @since 2.0.0
*/
export function compose<V extends Tag, A, I>(
second: Optic<V, A, I>,
): <U extends Tag, S>(
first: Optic<U, S, A>,
) => Optic<Align<U, V>, S, I> {
return <U extends Tag, S>(
first: Optic<U, S, A>,
): Optic<Align<U, V>, S, I> => {
const tag = align(first.tag, second.tag);
const _chain = getMonad(tag).chain;
const _first = _unsafeCast(first, tag);
const _second = _unsafeCast(second, tag);
const view = flow(_first, _chain(_second));
const modify = flow(second.modify, first.modify);
return optic(tag, view, modify);
};
}
/**
* Compose two reviewer functions, allowing one to create nested Reviewer
* structures.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import * as S from "./set.ts";
* import { pipe } from "./fn.ts";
*
* const set = <A>() => O.refold<ReadonlySet<A>, A>(
* Array.from,
* S.of,
* S.map,
* );
*
* const sets = pipe(
* set<ReadonlySet<number>>(),
* O.composeReviewer(set()),
* );
*
* const result = sets.review(1); // Set(Set(1))
* ```
*
* @since 2.0.0
*/
export function composeReviewer<A, I>(
second: Reviewer<A, I>,
): <S>(first: Reviewer<S, A>) => Reviewer<S, I> {
return (first) => reviewer((i) => first.review(second.review(i)));
}
/**
* Construct a Lens Viewer from a raw value A. The view function of this viewer
* operatates like constant(a).
*
* @example
* ```ts
* import * as O from "./optics.ts";
*
* const viewer = O.of(1);
*
* const result1 = viewer.view(2); // 1
* const result2 = viewer.view(100); // 1
* ```
*
* @since 2.0.0
*/
export function of<A, S = unknown>(a: A): Viewer<LensTag, S, A> {
return viewer(LensTag, (_: S) => a);
}
/**
* An invariant map over an Optic. If a type can be represented isomorphically
* by another type, one can imap to go back and forth.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import { pipe } from "./fn.ts";
*
* const plussed = pipe(
* O.id<number>(),
* O.imap(n => n + 100, n => n - 100),
* );
*
* const result1 = plussed.view(1); // 101
* const result2 = plussed.modify(n => n + 1)(1); // 2
* ```
*
* @since 2.0.0
*/
export function imap<A, I>(
fai: (a: A) => I,
fia: (i: I) => A,
): <U extends Tag, S>(
first: Optic<U, S, A>,
) => Optic<Align<U, LensTag>, S, I> {
return compose(iso(fai, fia));
}
/**
* Map over the Viewer portion of an optic. This effectively uses the map from
* the Monad associated with the tag of the optic.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import { pipe } from "./fn.ts";
*
* const mapped = pipe(
* O.id<ReadonlyArray<string>>(),
* O.index(1),
* O.map(str => str.length),
* );
*
* const result1 = mapped.view(["Hello", "World"]); // Some(5)
* const result2 = mapped.view([]); // None
* ```
*
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A) => I,
): <T extends Tag, S>(first: Viewer<T, S, A>) => Viewer<T, S, I> {
return ({ tag, view }) => {
const _map = getMonad(tag).map;
return viewer(tag, flow(view, _map(fai)));
};
}
/**
* Apply the value returned by a Viewer to a function returned by a Viewer.
*
* @example
* ```ts
* import * as O from "./optics.ts";
* import { pipe } from "./fn.ts";
*
* type Person = { name: string, age: number };
* type State = { people: readonly Person[], format: (p: Person) => string };
*
* const fmt = pipe(O.id<State>(), O.prop("format"));
* const adults = pipe(
* O.id<State>(),
* O.prop("people"),
* O.array,
* O.filter(p => p.age > 18)
* );
*
* const formatted = pipe(fmt, O.ap(adults));
*
* const result = formatted.view({
* people: [
* { name: "Brandon", age: 37 },
* { name: "Rufus", age: 1 },
* ],
* format: p => `${p.name} is ${p.age}`,
* }); // [ "Brandon is 37" ]
* ```
*
* @since 2.0.0
*/
export function ap<V extends Tag, S, A>(
second: Viewer<V, S, A> | Optic<V, S, A>,
): <U extends Tag, I>(
first: Viewer<U, S, (a: A) => I> | Optic<U, S, (a: A) => I>,
) => Viewer<Align<U, V>, S, I> {