-
Notifications
You must be signed in to change notification settings - Fork 6
/
set.ts
949 lines (908 loc) · 22.4 KB
/
set.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
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
/**
* ReadonlySet is a readonly product structure over objects
* and it operates on object equality for deduplication.
*
* @module ReadonlySet
* @since 2.0.0
*/
import type { $, Kind, Out } from "./kind.ts";
import type { Applicable } from "./applicable.ts";
import type { Combinable } from "./combinable.ts";
import type { Comparable } from "./comparable.ts";
import type { Either } from "./either.ts";
import type { Filterable } from "./filterable.ts";
import type { Bind, Flatmappable, Tap } from "./flatmappable.ts";
import type { Foldable } from "./foldable.ts";
import type { Initializable } from "./initializable.ts";
import type { BindTo, Mappable } from "./mappable.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 { Showable } from "./showable.ts";
import type { Traversable } from "./traversable.ts";
import type { Wrappable } from "./wrappable.ts";
import { flow, identity, pipe } from "./fn.ts";
import { fromCompare } from "./comparable.ts";
import { fromCombine } from "./combinable.ts";
import { createBind, createTap } from "./flatmappable.ts";
import { createBindTo } from "./mappable.ts";
import * as O from "./option.ts";
import * as E from "./either.ts";
/**
* Extract the inner type of a ReadonlySet
*
* @since 2.0.0
*/
export type TypeOf<T> = T extends ReadonlySet<infer A> ? A : never;
/**
* Specifies ReadonlySet as a Higher Kinded Type, with covariant
* parameter A corresponding to the 0th index of any substitutions.
*
* @since 2.0.0
*/
export interface KindReadonlySet extends Kind {
readonly kind: ReadonlySet<Out<this, 0>>;
}
/**
* Constructs a new ReadonlySet over type A that conuains no values.
*
* @example
* ```ts
* import * as S from "./set.ts";
*
* const result = S.init<number>(); // ReadonlySet<number> with no members.
* ```
*
* @since 2.0.0
*/
export function init<A = never>(): ReadonlySet<A> {
return new Set();
}
/**
* Constructs a new ReadonlySet<A> from an arbitrary number
* of values.
*
* @example
* ```ts
* import * as S from "./set.ts";
*
* const result = S.set(1, 2, 3); // ReadonlySet<number>
* ```
*
* @since 2.0.0
*/
export function set<A>(...as: readonly [A, ...A[]]): ReadonlySet<A> {
return new Set(as);
}
/**
* Copies an existing ReadonlySet into a new ReadonlySet, keeping
* references to the original members.
*
* @example
* ```ts
* import * as S from "./set.ts";
*
* const original = S.set(1, 2, 3);
* const copy = S.copy(original);
*
* const result1 = original === copy; // false
*
* const has = (value: number) => original.has(value) === copy.has(value);
*
* const result2 = has(1); // true;
* const result3 = has(10); // true;
* ```
*
* @since 2.0.0
*/
export function copy<A>(ua: ReadonlySet<A>): ReadonlySet<A> {
return new Set(ua);
}
/**
* Operates like Array.some, testing values in a ReadonlySet with a Predicate
* until either the predicate returns true for a value or all of the
* values have been tested. Shortcircuits on the first value that
* returns true. This is the dual of every.
*
* @example
* ```ts
* import * as S from "./set.ts";
*
* const some = S.some((n: number) => n > 0);
*
* const result1 = some(S.set(1, 2, 3)); // true
* const result2 = some(S.set(0)); // false
* const result3 = some(S.init()); // false
* const result4 = some(S.set(-1, -2, -3, 1)); // true
* ```
*
* @since 2.0.0
*/
export function some<A>(
predicate: Predicate<A>,
): (ua: ReadonlySet<A>) => boolean {
return (ua) => {
for (const a of ua) {
if (predicate(a)) {
return true;
}
}
return false;
};
}
/**
* Operates like Array.every, testing values in a ReadonlySet with a Predicate
* until either the predicate returns false for a value or all of the
* values have been tested as true. Shortcircuits on the first value that
* returns false. This is the dual of some.
*
* @example
* ```ts
* import * as S from "./set.ts";
*
* const some = S.some((n: number) => n > 0);
*
* const result1 = some(S.set(1, 2, 3)); // true
* const result2 = some(S.set(0)); // false
* const result3 = some(S.init()); // false
* const result4 = some(S.set(-1, -2, -3, 1)); // true
* ```
*
* @since 2.0.0
*/
export function every<A>(
predicate: Predicate<A>,
): (ua: ReadonlySet<A>) => boolean {
return (ua) => {
for (const a of ua) {
if (!predicate(a)) {
return false;
}
}
return true;
};
}
/**
* Given an insuance of Comparable<A> create a function
* that takes a value A and returns a predicate over
* ReadonlySet<A> the returns true if there are any
* members of the set that are equal to the value.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const elem = S.elem(N.ComparableNumber);
*
* const set = S.set(1, 2, 3);
*
* const result1 = pipe(set, elem(1)); // true
* const result2 = pipe(set, elem(10)); // false
* ```
*
* @since 2.0.0
*/
export function elem<A>(
{ compare }: Comparable<A>,
): (value: A) => (ua: ReadonlySet<A>) => boolean {
return (a) => some(compare(a));
}
/**
* Given an instance of Comparable<A> create a function
* that uakes a ReadonlySet<A> and returns a predicate over
* a value A the returns true if the value is a member
* of the set. This is like elem but with the set and value
* parameters swapped.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
*
* const elemOf = S.elemOf(N.ComparableNumber);
*
* const set = S.set(1, 2, 3);
* const inSet = elemOf(set);
*
* const result1 = inSet(1); // true
* const result2 = inSet(10); // false
* ```
*
* @since 2.0.0
*/
export function elemOf<A>(
S: Comparable<A>,
): (ua: ReadonlySet<A>) => (a: A) => boolean {
const _elem = elem(S);
return (ua) => (a) => _elem(a)(ua);
}
/**
* Given an instance of Comparable<A> return a function
* `second => first => boolean` that returns true when
* every member of first is in second.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const subset = S.isSubset(N.ComparableNumber);
*
* const big = S.set(1, 2, 3, 4, 5);
* const small = S.set(2, 4);
*
* const result1 = pipe(big, subset(small)); // false
* const result2 = pipe(small, subset(big)); // true;
* ```
*
* @since 2.0.0
*/
export function isSubset<A>(
S: Comparable<A>,
): (second: ReadonlySet<A>) => (first: ReadonlySet<A>) => boolean {
return flow(elemOf(S), every);
}
/**
* Given an instance of Comparable<A> return a function that takes
* two ReadonlySet<A>s and merges them into a new ReadonlySet<A>
* that contains all the elements from both sets.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const union = S.union(N.ComparableNumber);
* const s1 = S.set(1, 2, 3, 4);
* const s2 = S.set(3, 4, 5, 6);
*
* const result = pipe(s1, union(s2));
* // Set(1, 2, 3, 4, 5, 6)
* ```
*
* @since 2.0.0
*/
export function union<A>(
S: Comparable<A>,
): (second: ReadonlySet<A>) => (first: ReadonlySet<A>) => ReadonlySet<A> {
return (second) => (first) => {
const out = copy(first) as Set<A>;
const isIn = elemOf(S)(out);
for (const b of second) {
if (!isIn(b)) {
out.add(b);
}
}
return out;
};
}
/**
* Given an instance of Comparable<A> return a function that takes
* two ReadonlySet<A>s and returns a new set with only
* the elements that exist in both sets.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const intersect = S.intersection(N.ComparableNumber);
* const s1 = S.set(1, 2, 3, 4);
* const s2 = S.set(3, 4, 5, 6);
*
* const result = pipe(s1, intersect(s2));
* // Set(3, 4)
* ```
*
* @since 2.0.0
*/
export function intersection<A>(
S: Comparable<A>,
): (ua: ReadonlySet<A>) => (tb: ReadonlySet<A>) => ReadonlySet<A> {
return (ua) => {
const isIn = elemOf(S)(ua);
return (tb) => {
const out = new Set<A>();
for (const b of tb) {
if (isIn(b)) {
out.add(b);
}
}
return out;
};
};
}
/**
* Given an instance of Comparable<A> create a function that will
* take a ReadonlySet<A> and return a new ReadonlySet<A> where
* any members that are equal are deduplicated.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
* import * as E from "./comparable.ts";
*
* const eq = E.struct({ num: N.ComparableNumber });
* const compact = S.compact(eq);
*
* const set = S.set({ num: 1 }, { num: 1 }, { num: 2 });
* // Set({ num: 1 }, { num: 1 }, { num: 2 })
*
* const result = compact(set); // Set({ num: 1 }, { num: 2 })
* ```
*
* @since 2.0.0
*/
export function compact<A>(
S: Comparable<A>,
): (ua: ReadonlySet<A>) => ReadonlySet<A> {
return (ua) => {
const out = new Set<A>();
const isIn = elemOf(S)(out);
for (const a of ua) {
if (!isIn(a)) {
out.add(a);
}
}
return out;
};
}
/**
* Given a value A create a new ReadonlySet<A> that
* contains that value.
*
* @example
* ```ts
* import * as S from "./set.ts";
*
* const result = S.wrap(1); // Set(1);
* ```
*
* @since 2.0.0
*/
export function wrap<A>(a: A): ReadonlySet<A> {
return set(a);
}
/**
* Given a function A -> I and a ReadonlySet<A> return
* a new ReadonlySet<I> where the values were created
* by passing each A through the A -> I function.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import { pipe } from "./fn.ts";
*
* const set = S.set("hello", "world", "goodbye");
*
* const result = pipe(
* set,
* S.map(s => s.length),
* ); // Set(5, 7);
* ```
*
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A) => I,
): (ua: ReadonlySet<A>) => ReadonlySet<I> {
return (ua) => {
const ti = new Set<I>();
for (const a of ua) {
ti.add(fai(a));
}
return ti;
};
}
/**
* Given a ReadonlySet of functions A -> I and
* a ReadonlySet<A> return a ReadonlySet<I> by applying
* every function to every value A.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import { pipe } from "./fn.ts";
*
* type Person = { name: string, age: number };
*
* const person = (name: string) => (age: number): Person => ({ name, age });
*
* const result = pipe(
* S.wrap(person),
* S.apply(S.wrap("Brandon")),
* S.apply(S.wrap(37)),
* ); // ReadonlySet<Person>
* ```
*
* @since 2.0.0
*/
export function apply<A>(
ua: ReadonlySet<A>,
): <I>(ufai: ReadonlySet<(a: A) => I>) => ReadonlySet<I> {
return <I>(ufai: ReadonlySet<(a: A) => I>): ReadonlySet<I> => {
const ti = new Set<I>();
for (const a of ua) {
for (const fai of ufai) {
ti.add(fai(a));
}
}
return ti;
};
}
/**
* Given a function A -> ReadonlySet<I> and a ReadonlySet<A>
* return a ReadonlySet<I> created by applying the function
* to every value A and joining all the resulting ReadonlySet<I>s.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import { pipe } from "./fn.ts";
*
* const set = S.set(1, 2, 3, 4, 5);
*
* const result = pipe(
* set,
* S.flatmap(n => S.set(n, n + 1, n + 2)),
* ); // Set(1, 2, 3, 4, 5, 6, 7);
* ```
*
* @since 2.0.0
*/
export function flatmap<A, I>(
fati: (a: A) => ReadonlySet<I>,
): (ua: ReadonlySet<A>) => ReadonlySet<I> {
return (ua) => {
const ti = new Set<I>();
for (const a of ua) {
const _ti = fati(a);
for (const i of _ti) {
ti.add(i);
}
}
return ti;
};
}
/**
* Given a ReadonlySet of ReadonlySet<A>, flatten all of the inner
* sets and return a ReadonlySet<A>.
*
* @example
* ```ts
* import * as S from "./set.ts";
*
* const setOfSets = S.set(S.set(1, 2), S.set(3, 4), S.set(1, 4));
*
* const result = S.join(setOfSets); // Set(1, 2, 3, 4)
* ```
*
* @since 2.0.0
*/
export function join<A>(uua: ReadonlySet<ReadonlySet<A>>): ReadonlySet<A> {
return pipe(uua, flatmap(identity));
}
/**
* Given a Refinement or Predicate over A and a ReadonlySet<A> return
* a new ReadonlySet with only values for which the predicate or
* refinement return true.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import { pipe } from "./fn.ts";
*
* const set = S.set(1, 2, 3, 4, 5);
*
* const result1 = pipe(set, S.filter(n => n > 2)); // Set(3, 4, 5)
* const result2 = pipe(set, S.filter(n => n === 0)); // Set()
* ```
*
* @since 2.0.0
*/
export function filter<A, B extends A>(
refinement: Refinement<A, B>,
): (ua: ReadonlySet<A>) => ReadonlySet<B>;
export function filter<A>(
predicate: Predicate<A>,
): (ua: ReadonlySet<A>) => ReadonlySet<A>;
export function filter<A>(
predicate: Predicate<A>,
): (ua: ReadonlySet<A>) => ReadonlySet<A> {
return (ua) => {
const _ua = new Set<A>();
for (const a of ua) {
if (predicate(a)) {
_ua.add(a);
}
}
return _ua;
};
}
/**
* Given a function A -> Option<I> and a ReadonlySet<A> return
* a ReadonlySet<I> by applying the function to all values A. Any
* Nones will not enter the resultant set while Some<I> values will.
* This is effectively filtering and mapping simultaneously.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as O from "./option.ts";
* import { pipe } from "./fn.ts";
*
* const set = S.set("one", "two", "three", "four", "five");
*
* const result = pipe(
* set,
* S.filterMap(s => s.includes('o') ? O.some(s.length) : O.none),
* ); // Set(3, 4)
* ```
*
* @since 2.0.0
*/
export function filterMap<A, I>(
fai: (a: A) => Option<I>,
): (ua: ReadonlySet<A>) => ReadonlySet<I> {
return (ua) => {
const output = new Set<I>();
for (const a of ua) {
const value = fai(a);
if (O.isSome(value)) {
output.add(value.value);
}
}
return output;
};
}
/**
* Given a Predicate or Refinement over A and a ReadonlySet<A>
* return a Pair with a first value being a ReadonlySet of values
* that return true when applied to the refinement or predicate
* and a second value being a ReadonlySet of values that return
* false when applied to the predicate.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import { pipe } from "./fn.ts";
*
* const set = S.set(1, 2, 3, 4);
*
* const result = pipe(
* set,
* S.partition(n => n > 2),
* ); // [Set(3, 4), Set(1, 2)]
* ```
*
* @since 2.0.0
*/
export function partition<A, B extends A>(
refinement: Refinement<A, B>,
): (ua: ReadonlySet<A>) => Pair<ReadonlySet<B>, ReadonlySet<A>>;
export function partition<A>(
predicate: Predicate<A>,
): (ua: ReadonlySet<A>) => Pair<ReadonlySet<A>, ReadonlySet<A>>;
export function partition<A>(
predicate: Predicate<A>,
): (ua: ReadonlySet<A>) => Pair<ReadonlySet<A>, ReadonlySet<A>> {
return (ua) => {
const first = new Set<A>();
const second = new Set<A>();
for (const a of ua) {
if (predicate(a)) {
first.add(a);
} else {
second.add(a);
}
}
return [first, second];
};
}
/**
* Given a function A -> Either<J, I> and a ReadonlySet<A> return
* a Pair(ReadonlySet<I>, ReadonlySet<J>) by applying every value A
* in the set to the partitioning function.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as E from "./either.ts";
* import { pipe } from "./fn.ts";
*
* const set = S.set("one", "two", "three", "four");
*
* const result = pipe(
* set,
* S.partitionMap(s => s.includes('o') ? E.right(s) : E.left(s.length)),
* ); // [Set("one", "two", "four"), Set(5)]
* ```
*
* @since 2.0.0
*/
export function partitionMap<A, I, J>(
fai: (a: A) => Either<J, I>,
): (ua: ReadonlySet<A>) => Pair<ReadonlySet<I>, ReadonlySet<J>> {
return (ua) => {
const first = new Set<I>();
const second = new Set<J>();
for (const a of ua) {
const result = fai(a);
if (E.isRight(result)) {
first.add(result.right);
} else {
second.add(result.left);
}
}
return [first, second];
};
}
/**
* Reduce a ReadonlySet<A> to a value O by iterating over
* the values of the set and collecting them with the reducing function.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import { pipe } from "./fn.ts";
*
* const set = S.set(1, 2, 3, 4);
*
* const result = pipe(
* set,
* S.fold((previous, current) => previous + current, 0),
* ); // 10
* ```
*
* @since 2.0.0
*/
export function fold<A, O>(
foao: (o: O, a: A) => O,
o: O,
): (ua: ReadonlySet<A>) => O {
return (ua) => {
let out = o;
for (const a of ua) {
out = foao(out, a);
}
return out;
};
}
// This is an unsafe Add for ReadonlySet<A> that violates Readonly contract
const unsafeAdd = <A>(ua: ReadonlySet<A>) => (a: A): Set<A> => {
(ua as Set<A>).add(a);
return ua as Set<A>;
};
/**
* Traverse a ReadonlySet<A> value by value, applying a function
* A -> V<I>, then collecting all of the I values into ReadonlySet<I>
* and returning V<ReadonlySet<I>>. In concrete terms this can take
* ReadonlySet<Option<A>> and turn it into Option<ReadonlySet<I>> and
* other ADT inversions.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as O from "./option.ts";
* import { pipe } from "./fn.ts";
*
* const traverseOption = S.traverse(O.ApplicableOption);
* const invert = traverseOption((o: O.Option<number>) => o);
*
* const result1 = pipe(
* S.set(O.some(1), O.some(2), O.some(3)),
* invert,
* ); // Some(Set(1, 2, 3))
* const result2 = pipe(
* S.set(O.some(1), O.some(2), O.none),
* invert,
* ); // None
* ```
*
* @since 2.0.0
*/
export function traverse<V extends Kind>(
A: Applicable<V>,
): <A, I, J, K, L, M>(
favi: (a: A) => $<V, [I, J, K], [L], [M]>,
) => (ua: ReadonlySet<A>) => $<V, [ReadonlySet<I>, J, K], [L], [M]> {
return <A, I, J, K, L, M>(
favi: (a: A) => $<V, [I, J, K], [L], [M]>,
): (ua: ReadonlySet<A>) => $<V, [ReadonlySet<I>, J, K], [L], [M]> =>
fold(
(vis, a) => pipe(vis, A.map(unsafeAdd), A.apply(favi(a))),
A.wrap(init() as Set<I>),
);
}
/**
* Given an instance of Showable<A> return an instance of Showable<ReadonlySet<A>>.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
*
* const { show } = S.getShowableSet(N.ShowableNumber);
*
* const result1 = show(S.init()); // "Set([])"
* const result2 = show(S.set(1, 2, 3)); // "Set([1, 2, 3])"
* ```
*
* @since 2.0.0
*/
export function getShowableSet<A>(S: Showable<A>): Showable<ReadonlySet<A>> {
return ({
show: (s) => `Set([${Array.from(s.values()).map(S.show).join(", ")}])`,
});
}
/**
* Given an instance of Comparable<A> return Comparable<ReadonlySet<A>>.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
* import { pipe } from "./fn.ts";
*
* const { compare } = S.getComparableSet(N.ComparableNumber);
*
* const result1 = compare(
* S.set(1, 2, 3))(
* S.set(3, 2, 1)
* ); // true
* const result2 = compare(
* S.set(1, 2, 3))(
* S.set(1, 2, 3, 4)
* ); // false
* ```
*
* @since 2.0.0
*/
export function getComparableSet<A>(
S: Comparable<A>,
): Comparable<ReadonlySet<A>> {
const subset = isSubset(S);
return fromCompare((second) => (first) =>
subset(first)(second) && subset(second)(first)
);
}
/**
* Given an instance of Comparable<A> create a Combinable<ReadonlySet<A>> where
* combine creates a union of two ReadonlySets.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
* import * as M from "./combinable.ts";
* import { pipe } from "./fn.ts";
*
* const monoid = S.getCombinableSet(N.ComparableNumber);
* const combineAll = M.getCombineAll(monoid);
*
* const result1 = combineAll(
* S.set(1, 2, 3),
* S.set(4, 5, 6),
* S.set(1, 3, 5, 7)
* ); // Set(1, 2, 3, 4, 5, 6, 7)
* const result3 = combineAll(S.init()); // Set()
* ```
*
* @since 2.0.0
*/
export function getCombinableSet<A>(
S: Comparable<A>,
): Combinable<ReadonlySet<A>> {
return fromCombine(union(S));
}
/**
* Given an instance of Comparable<A> create a Combinable<ReadonlySet<A>> where
* combine creates a union of two ReadonlySets.
*
* @example
* ```ts
* import * as S from "./set.ts";
* import * as N from "./number.ts";
* import * as M from "./combinable.ts";
* import { pipe } from "./fn.ts";
*
* const monoid = S.getInitializableSet(N.ComparableNumber);
* const combineAll = M.getCombineAll(monoid);
*
* const result1 = combineAll(
* S.set(1, 2, 3),
* S.set(4, 5, 6),
* S.set(1, 3, 5, 7)
* ); // Set(1, 2, 3, 4, 5, 6, 7)
* const result3 = combineAll(S.init()); // Set()
* ```
*
* @since 2.0.0
*/
export function getInitializableSet<A>(
C: Comparable<A>,
): Initializable<ReadonlySet<A>> {
return {
init: () => new Set(),
...getCombinableSet(C),
};
}
/**
* The canonical implementation of Applicable for ReadonlySet. It contains
* the methods wrap, ap, and map.
*
* @since 2.0.0
*/
export const ApplicableSet: Applicable<KindReadonlySet> = { apply, map, wrap };
/**
* The canonical implementation of Filterable for ReadonlySet. It contains
* the methods filter, filterMap, partition, and partitionMap.
*
* @since 2.0.0
*/
export const FilterableSet: Filterable<KindReadonlySet> = {
filter,
filterMap,
partition,
partitionMap,
};
/**
* The canonical implementation of Flatmappable for ReadonlySet. It contains
* the methods wrap, ap, map, join, and flatmap.
*
* @since 2.0.0
*/
export const FlatmappableSet: Flatmappable<KindReadonlySet> = {
apply,
flatmap,
map,
wrap,
};
/**
* The canonical implementation of Foldable for ReadonlySet. It contains
* the method fold.
*
* @since 2.0.0
*/
export const FoldableSet: Foldable<KindReadonlySet> = { fold };
/**
* The canonical implementation of Mappable for ReadonlySet. It contains
* the method map.
*
* @since 2.0.0
*/
export const MappableSet: Mappable<KindReadonlySet> = { map };
/**
* The canonical implementation of Traversable for ReadonlySet. It contains
* the methods map, fold, and traverse.
*
* @since 2.0.0
*/
export const TraversableSet: Traversable<KindReadonlySet> = {
map,
fold,
traverse,
};
/**
* @since 2.0.0
*/
export const WrappableSet: Wrappable<KindReadonlySet> = { wrap };
/**
* @since 2.0.0
*/
export const tap: Tap<KindReadonlySet> = createTap(FlatmappableSet);
/**
* @since 2.0.0
*/
export const bind: Bind<KindReadonlySet> = createBind(FlatmappableSet);
/**
* @since 2.0.0
*/
export const bindTo: BindTo<KindReadonlySet> = createBindTo(MappableSet);