This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
set.ts
216 lines (184 loc) · 5.26 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
import type * as HKT from "./hkt.ts";
import type * as TC from "./type_classes.ts";
import type { Fn, Predicate } from "./types.ts";
import { pipe } from "./fns.ts";
import { _reduce } from "./array.ts";
import { fromEquals } from "./setoid.ts";
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "Set";
export type URI = typeof URI;
declare module "./hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: Set<_[0]>;
}
}
/*******************************************************************************
* Constructors
******************************************************************************/
export const zero: Set<never> = new Set();
export const empty = <A = never>(): Set<A> => new Set();
export const make = <A>(...as: [A, ...A[]]): Set<A> => new Set(as);
/*******************************************************************************
* Utilities
******************************************************************************/
export const some = <A>(
predicate: Predicate<A>,
) =>
(set: Set<A>): boolean => {
for (const a of set) {
if (predicate(a)) {
return true;
}
}
return false;
};
export const every = <A>(
predicate: Predicate<A>,
) =>
(set: Set<A>): boolean => {
for (const a of set) {
if (!predicate(a)) {
return false;
}
}
return true;
};
export const elem = <A>(S: TC.Setoid<A>) => (a: A) => some(S.equals(a));
export const elemOf = <A>(S: TC.Setoid<A>) =>
(set: Set<A>) => (a: A) => elem(S)(a)(set);
export const isSubset = <A>(S: TC.Setoid<A>) =>
(set: Set<A>) => every(elemOf(S)(set));
export const union = <A>(S: TC.Setoid<A>) =>
(as: Set<A>) =>
(bs: Set<A>): Set<A> => {
const out = new Set(as);
const isIn = elemOf(S)(out);
for (const b of bs) {
if (!isIn(b)) {
out.add(b);
}
}
return out;
};
export const intersection = <A>(S: TC.Setoid<A>) =>
(ta: Set<A>) =>
(tb: Set<A>): Set<A> => {
const out = new Set<A>();
const isIn = elemOf(S)(ta);
for (const b of tb) {
if (isIn(b)) {
out.add(b);
}
}
return out;
};
export const compact = <A>(S: TC.Setoid<A>) =>
(ta: Set<A>): Set<A> => {
const out = new Set<A>();
const isIn = elemOf(S)(out);
for (const a of ta) {
if (!isIn(a)) {
out.add(a);
}
}
return out;
};
export const join = <A>(tta: Set<Set<A>>): Set<A> => {
const out = new Set<A>();
for (const ta of tta) {
for (const a of ta) {
out.add(a);
}
}
return out;
};
/*******************************************************************************
* Modules
******************************************************************************/
export const Functor: TC.Functor<URI> = {
map: <A, B>(fab: (a: A) => B) =>
(ta: Set<A>): Set<B> => {
const out = new Set<B>();
for (const a of ta) {
out.add(fab(a));
}
return out;
},
};
export const Apply: TC.Apply<URI> = {
ap: <A, B>(tfab: Set<(a: A) => B>) =>
(ta: Set<A>): Set<B> => {
const out = new Set<B>();
for (const fab of tfab) {
for (const a of ta) {
out.add(fab(a));
}
}
return out;
},
map: Functor.map,
};
export const Filterable: TC.Filterable<URI> = {
filter: <A>(predicate: Predicate<A>) =>
(ta: Set<A>): Set<A> => {
const out: Set<A> = new Set();
for (const a of ta) {
if (predicate(a)) {
out.add(a);
}
}
return out;
},
};
export const Foldable: TC.Foldable<URI> = {
reduce: <A, B>(faba: Fn<[A, B], A>, a: A) =>
(tb: Set<B>): A => _reduce(Array.from(tb), faba, a),
};
export const Traversable: TC.Traversable<URI> = {
map: Functor.map,
reduce: Foldable.reduce,
traverse: (A) =>
(faub) =>
(ta) =>
pipe(
ta,
Foldable.reduce(
(fbs, a) =>
pipe(
faub(a),
A.ap(pipe(
fbs,
A.map((bs) =>
(b) => {
bs.add(b);
return bs;
}
),
)),
),
A.of(new Set()),
),
),
};
/*******************************************************************************
* Module Getters
******************************************************************************/
export const getShow = <A>(S: TC.Show<A>): TC.Show<Set<A>> => ({
show: (s) => `Set([${Array.from(s.values()).map(S.show).join(", ")}])`,
});
export const getSetoid = <A>(S: TC.Setoid<A>): TC.Setoid<Set<A>> => {
const subset = isSubset(S);
return fromEquals((x) => (y) => subset(x)(y) && subset(y)(x));
};
export const getUnionMonoid = <A>(S: TC.Setoid<A>): TC.Monoid<Set<A>> => ({
concat: union(S),
empty,
});
/*******************************************************************************
* Pipeables
******************************************************************************/
export const { filter } = Filterable;
export const { map, reduce, traverse } = Traversable;